【问题标题】:Pandas read_csv() parses dates fine but can't index by datePandas read_csv() 可以很好地解析日期,但不能按日期索引
【发布时间】:2021-07-23 02:14:51
【问题描述】:

这很奇怪。

数据(csv):

Date,    Hr 1,Hr 2,Hr 3,..
20070701,1128,1072,1173,..
20070702,1131,1092,1287,..

pd.read_csv() 的普通用法:

df = pd.read_csv(   filename,
                    parse_dates=['Date'],
                    index_col=['Date'])

日期似乎可以很好地解析到索引中:

print(df.index[:2])

输出:

DatetimeIndex(['2007-07-01', '2007-07-02'], dtype='datetime64[ns]', name='Date', freq=None)

现在如果我尝试索引一天?

print(df['2007-7-1']) # or any variation on "2007-07-01" etc

输出:

Traceback (most recent call last):
  File "/Users/mjw/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2646, in get_loc
    return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: '2007-7-1'
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "my_file.py", line 108, in <module>
    print(df['2007-7-1'])
  File "/Users/mjw/opt/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py", line 2800, in __getitem__
    indexer = self.columns.get_loc(key)
  File "/Users/mjw/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2648, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: '2007-7-1'

我还尝试确保 DatetimeIndex 频率设置正确

df = df.asfreq('d')

我得到了同样的垃圾。

但是按年和月索引可以正常工作,或者在选择列后按年-月-日索引:

print(df['2007-7']) # works
print(df['Hr 1']['2007-7-1']) # works

但这不是:

print(df['2007-7-1']['Hr 1'])

我可以制作自定义日期解析器,但重点是我不应该这样做。 “yyyymmdd”并不难或不寻常。来大熊猫吧。

请,谢谢!

【问题讨论】:

  • 尝试df[:'2007-07-01'].head(2)df['2007-07-01':'2007-07-01'].head(2)

标签: python pandas csv datetime


【解决方案1】:

使用.loc:

print(df.loc["2007-07-01"])

打印:

    Hr 1    1128
Hr 2        1072
Hr 3        1173
Name: 2007-07-01 00:00:00, dtype: int64

对于“Hr 2”列的值:

print(df.loc["2007-07-01", "Hr 2"])

打印:

1072

【讨论】:

  • 那行得通。有什么不同。我在其他地方一直成功地使用df['2007-7-1']
  • @Mike 使用df['2007-7-1'],您正在选择名为2007-7-1的列
  • 那么为什么df['2007-7'] 会返回所有列,而只返回2007-7-12007-7-31 的行?换一种说法:没有名为“2007-7-1”的列只有(一)行。
  • @Mike 使用 df['2007-7'] 是 pandas 已弃用的功能。我的版本写/usr/src/app/example.py:51: FutureWarning: Indexing a DataFrame with a datetimelike index using a single string to slice the rows, like 'frame[string]', is deprecated and will be removed in a future version. Use 'frame.loc[string]' instead.正确的方法是使用.loc
猜你喜欢
  • 2023-03-17
  • 2018-10-10
  • 2017-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-30
  • 2013-02-20
相关资源
最近更新 更多