1

This code is giving me the following error in Python 3.6 pandas 23.x

IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

on line

ostype[asset] = dfx.ostype(0) # get same error with [0]

I'm not clear why. Here is the entire code, running in Jupyter.

from collections import defaultdict
assets = defaultdict(list)
warnings = defaultdict(list)
result = defaultdict(list)
for asset in servers:
    result[asset] = 'Fail'
    # This filters to only the records for this asset
    dfx = df[df['server'] == asset]
    ostype[asset] = dfx.ostype(0) ### Error
    for ntp in dfx.source:
        if ntp in valid_ntp_servers:
            result[asset] = 'Pass'
            assets[asset].append(ntp)
        else:
            warnings[asset].append(ntp)

I put the code in a separate Jupyter cell and get exactly the expected result

test = dfx.ostype[0]
print(test)

linux

Additional testing

for asset in servers[:5]:
    dfx = df[df['server'] == asset]
    test = dfx.ostype[0]
    print(dfx)
    print(test)

Results in the following exception

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-388-010067e6d390> in <module>()
      2 for asset in servers[:5]:
      3     dfx = df[df['server'] == asset]
----> 4     test = dfx.ostype[0]
      5     print(dfx)
      6     print(test)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
    765         key = com._apply_if_callable(key, self)
    766         try:
--> 767             result = self.index.get_value(self, key)
    768 
    769             if not is_scalar(result):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
   3116         try:
   3117             return self._engine.get_value(s, k,
-> 3118                                           tz=getattr(series.dtype, 'tz', None))
   3119         except KeyError as e1:
   3120             if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 0

The output from the loop looks like:

  ostype        date   server            source
0  linux  2018-08-14  abcde01  east.time.zzzzzz
1  linux  2018-08-14  abcde01       ntp7.zzzzzz
2  linux  2018-08-14  abcde01       ntp9.zzzzzz
3  linux  2018-08-14  abcde01       ntp0.zzzzzz
linux

Using .iloc[0] as suggested in the comments works in my test loop, but fails when I use it in my program.

IndexError                                Traceback (most recent call last)
<ipython-input-390-fce3c88d6e8e> in <module>()
      6     result[asset] = 'Fail'
      7     dfx = df[df['server'] == asset]
----> 8     ostype[asset] = dfx.ostype.iloc[0]
      9     for ntp in dfx.source:
     10         if ntp in valid_ntp_servers:

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
3
  • 1
    Try to use [0] instead of (0). Commented Aug 28, 2018 at 3:37
  • @Shaido I am pretty sure you just caught it :) Commented Aug 28, 2018 at 3:37
  • @Shaido I tried that too, with the same error. I tried every variant (.loc, astype, etc.) I know that should work w/o success and can't explain why. It works until I put it into a loop, then I start getting keyerror or indexerror Commented Aug 28, 2018 at 12:58

1 Answer 1

1

The bug was a loose nut on the keyboard.

This line

ostype[asset] = dfx.ostype.iloc[0]

was throwing various index and key exceptions and we focused on the right hand side of the equation. The problem is on the left. "ostype" is also a field name in a dataframe that was hastily changed when the original name, "type" caused problems.

Corners and doorways folks. Watch them corners and doorways. Or in this case, both sides of the equation.

Thanks

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.