Панды ссылаются на столбец индекса по имени - PullRequest
0 голосов
/ 18 сентября 2018

Я использую панд pandas-0.23.4-cp36-cp36m-manylinux1_x86_64.whl, и я заметил, что когда вы устанавливаете столбец в качестве столбца индекса, вы больше не можете ссылаться на него по имени.Можно ли ссылаться на столбец после того, как вы установили его в качестве индекса?Приведенный ниже код поднимает KeyError.

import pandas as pd
from datetime import datetime, timedelta
df = pd.DataFrame()

one_month = datetime.now() - timedelta(days=30)
ts_index = pd.date_range(one_month, periods=30, freq='1D')

df.insert(0, 'tscol', ts_index)
df.insert(1, 'value', 1.0)

print(df.head())

# set the timeseries column as the index.
df.set_index('tscol', inplace=True)

print(df.head())

for index, row in df.iterrows():
    print(row['tscol'])
    break

Здесь вы можете увидеть кадр данных до и после того, как tscol станет индексом:

До

                       tscol  value
0 2018-08-19 10:53:32.412154    1.0
1 2018-08-20 10:53:32.412154    1.0
2 2018-08-21 10:53:32.412154    1.0
3 2018-08-22 10:53:32.412154    1.0
4 2018-08-23 10:53:32.412154    1.0

После

                            value
tscol                            
2018-08-19 10:53:32.412154    1.0
2018-08-20 10:53:32.412154    1.0
2018-08-21 10:53:32.412154    1.0
2018-08-22 10:53:32.412154    1.0
2018-08-23 10:53:32.412154    1.0

Дает мне это исключение

Traceback (most recent call last):
  File "/home/ben/.local/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3124, in get_value
    return libindex.get_value_box(s, key)
  File "pandas/_libs/index.pyx", line 55, in pandas._libs.index.get_value_box
  File "pandas/_libs/index.pyx", line 63, in pandas._libs.index.get_value_box
TypeError: 'str' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "index_by_name.py", line 24, in <module>
    print(row['tscol'])
  File "/home/ben/.local/lib/python3.6/site-packages/pandas/core/series.py", line 767, in __getitem__
    result = self.index.get_value(self, key)
  File "/home/ben/.local/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3132, in get_value
    raise e1
  File "/home/ben/.local/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3118, in get_value
    tz=getattr(series.dtype, 'tz', None))
  File "pandas/_libs/index.pyx", line 106, in pandas._libs.index.IndexEngine.get_value
  File "pandas/_libs/index.pyx", line 114, in pandas._libs.index.IndexEngine.get_value
  File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'tscol'

1 Ответ

0 голосов
/ 18 сентября 2018

Вы можете передать аргумент drop=False при настройке индекса для сохранения его в виде столбца в DataFrame:

df.set_index('tscol', inplace=True, drop=False)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...