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
[0]instead of(0).