Python Pandas 日期时间索引

标签 python datetime pandas reindex

在 DataFrame 的 DateTimeIndex 之下。夏季/冬季时区是“美国/东部”,但记录的时间戳是“欧洲/伦敦”。我正在尝试重新索引,这将实现完整的时间序列。

DatetimeIndex(['1993-10-24 21:00:00', '1993-10-25 21:00:00',
               '1993-10-26 21:00:00', '1993-10-27 21:00:00',
               '1993-10-28 21:00:00', '1993-10-31 22:00:00',
               '1993-11-01 22:00:00', '1993-11-02 22:00:00',
               '1993-11-03 22:00:00', '1993-11-04 22:00:00'],
              dtype='datetime64[ns]', name=u'TIME', freq=None)

如何在不弄乱小时元素的情况下重新索引上述内容?

最佳答案

tidx = pd.DatetimeIndex(
    [
        '1993-10-24 21:00:00', '1993-10-25 21:00:00',
        '1993-10-26 21:00:00', '1993-10-27 21:00:00',
        '1993-10-28 21:00:00', '1993-10-31 22:00:00',
        '1993-11-01 22:00:00', '1993-11-02 22:00:00',
        '1993-11-03 22:00:00', '1993-11-04 22:00:00'],
    dtype='datetime64[ns]', name=u'TIME', freq=None
)

ts = tidx.to_series().dt.hour.resample('D').last().ffill().rename_axis('date')
ts.index + pd.to_timedelta(ts.values, unit='H')

DatetimeIndex(['1993-10-24 21:00:00', '1993-10-25 21:00:00',
               '1993-10-26 21:00:00', '1993-10-27 21:00:00',
               '1993-10-28 21:00:00', '1993-10-29 21:00:00',
               '1993-10-30 21:00:00', '1993-10-31 22:00:00',
               '1993-11-01 22:00:00', '1993-11-02 22:00:00',
               '1993-11-03 22:00:00', '1993-11-04 22:00:00'],
              dtype='datetime64[ns]', freq=None)

关于Python Pandas 日期时间索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40249941/

相关文章:

python - 在大型 PDF 文件中搜索。使用什么技术?

java网络启动时间

python - Pandas:将填充有下拉菜单的 Excel 列拆分为多个数据框列并隔离拼写错误

python - 如何替换 dataframe 中列的某些值中的字符 '...'。 '.' 的个数不固定。

Python + Qt : pyqtProperty, 样式表和事件处理程序

python lxml解析html

datetime - 时间格式 - Coldfusion 9

java - 无法使用 Joda 时间将 DateTime 转换为 ReadableInstant

python - 如何在 pandas loc API 中使用 *and*

python运行时错误,可以转储文件吗?