【问题标题】:Datetime does not work in my index series日期时间在我的索引系列中不起作用
【发布时间】:2016-09-01 04:45:51
【问题描述】:

我有一个充满一些数据的 hf5。

当我在 python 中打开它时,我有这个输出

hf['SB'].head()

0_1                     price   cng     
2015-07-15 07:30:00.087 12.61   4
2015-07-15 07:30:00.087 12.61   1
2015-07-15 07:30:00.087 12.61   1
2015-07-15 07:30:00.087 12.61   2
2015-07-15 07:30:00.087 12.61   19

此文件的范围从 2015 年到 2016 年。

hf['SB'].index

DatetimeIndex(['2015-07-15 07:30:00.087000', '2015-07-15 07:30:00.087000',
               '2015-07-15 07:30:00.087000', '2015-07-15 07:30:00.087000',
               '2015-07-15 07:30:00.087000', '2015-07-15 07:30:00.087000',
               '2015-07-15 07:30:00.087000', '2015-07-15 07:30:00.087000',
               '2015-07-15 07:30:00.087000', '2015-07-15 07:30:00.087000',
               ...
               '2016-07-14 16:59:57.670000', '2016-07-14 16:59:58.047000',
               '2016-07-14 16:59:59.170000', '2016-07-14 16:59:59.170000',
               '2016-07-14 16:59:59.170000', '2016-07-14 16:59:59.170000',
               '2016-07-14 16:59:59.170000', '2016-07-14 16:59:59.170000',
               '2016-07-14 16:59:59.170000', '2016-07-14 16:59:59.957000'],
              dtype='datetime64[ns]', name=u'0_1', length=3961015, freq=None)

嗯...我的问题是:

当我想要切片时,例如 2015 年 8 月 20 日:

hf['SB'][datetime(2015,8,20)]

我收到此错误:KeyError: datetime.datetime(2015, 8, 20, 0, 0)

但如果我使用:

hf['SB']['2015-08-20']

它的作品!!!

我的索引文件有问题还是我使用了datetime 函数错误?

【问题讨论】:

  • 尝试将datetime(2015,8,20) 转换为字符串,例如hf['SB']['{}'.format(datetime(2015,8,20)]
  • Same error: KeyError: '2015-08-20 00:00:00' 我认为这个错误是因为在转换为字符串时需要精确的小时和分钟,例如 00:00:00 .由于我的数据没有这个确切的值,我得到了这个错误
  • 试试hf['SB']['{}'.format(datetime(2015,8,20).date()]
  • 嘿,它的工作... =] 但我不明白为什么简单的“日期时间(2015,8,20)”不起作用。去年我使用了同样的想法并且工作得很好。也许功能改变了......嗯,谢谢你的建议

标签: python datetime pandas


【解决方案1】:

我相信,您正在为一个根本不存在于索引中的键获得价值。 hf['SB']['2015-08-20'] 将为您提供该特定日期的所有记录。 请参见下面的示例:

>>> rng = pd.date_range('1/1/2016', periods=10, freq='S')
>>> ts = pd.Series(np.random.randn(len(rng)), index=rng)
>>> ts = ts[1:]

>>> ts
2016-01-01 00:00:01    0.133551
2016-01-01 00:00:02    1.067772
2016-01-01 00:00:03    0.591676
2016-01-01 00:00:04   -2.445586
2016-01-01 00:00:05    0.700155
2016-01-01 00:00:06   -0.127861
2016-01-01 00:00:07    1.116494
2016-01-01 00:00:08   -0.427959
2016-01-01 00:00:09    2.115352
Freq: S, dtype: float64

>>> ts[datetime.date(2016,1,1)]
....
KeyError: datetime.date(2016, 1, 1)

>>>  ts['2016-01-01']

2016-01-01 00:00:01    0.133551
2016-01-01 00:00:02    1.067772
2016-01-01 00:00:03    0.591676
2016-01-01 00:00:04   -2.445586
2016-01-01 00:00:05    0.700155
2016-01-01 00:00:06   -0.127861
2016-01-01 00:00:07    1.116494
2016-01-01 00:00:08   -0.427959
2016-01-01 00:00:09    2.115352
Freq: S, dtype: float64

所以使用hf['SB']['2015-08-20'],您将获得“2015-08-20”的所有记录,但没有获得datetime.datetime(2015,8,20,0,0) 的所有记录

如果您想要使用日期时间的切片,请尝试以下操作:

>>> ts[datetime.datetime(2016,1,1,0,0,1):datetime.datetime(2016,1,1,0,0,3)]

2016-01-01 00:00:01    0.133551
2016-01-01 00:00:02    1.067772
2016-01-01 00:00:03    0.591676
Freq: S, dtype: float64

【讨论】:

  • 是的,我认为你是对的。但是如果我想要一个星期的切片,例如你的建议失败,因为我需要知道从第一个点到最后一个点的确切时间。你能建议另一种方法来分割这类信息吗??
  • 如果你想切片,你应该知道切片范围 - 检查link 以获取其他示例。还可以查看link,了解更多关于 DateTimeIndex 可用的属性和方法的信息(例如周数)。
猜你喜欢
  • 2020-05-27
  • 2011-04-30
  • 2018-01-24
  • 2021-12-29
  • 1970-01-01
  • 1970-01-01
  • 2019-02-19
  • 2012-08-24
  • 1970-01-01
相关资源
最近更新 更多