【问题标题】:Python Pandas DatetimeIndex.hourPython Pandas DatetimeIndex.hour
【发布时间】:2018-07-13 07:09:08
【问题描述】:

我正在尝试在我的数据框中为带有 DatetimeIndex 的时间戳 HOUR、DAY、MONTH 的值构建 3 个单独的列。

我为无法复制的数据道歉,因为我的数据集是从 CSV 文件中读取的。

boilerDf = pd.read_csv('C:\\Users\\Python Scripts\\Deltadata.csv', index_col='Date', parse_dates=True)

print(boilerDf.info())

这会返回:

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 23797 entries, 2017-10-25 05:00:08.436000 to 2018-01-02 05:45:14.419000
Data columns (total 3 columns):
hwr    23797 non-null float64
hws    23797 non-null float64
oat    23797 non-null float64
dtypes: float64(3)
memory usage: 743.7 KB
None

我可以在 pandas.pydata.org 网站上看到它们是我想要做的 3 种方法,除了我想创建单独的数据框(列):

DatetimeIndex.month 
DatetimeIndex.day   
DatetimeIndex.hour  

以下代码不适用于为日期时间索引的小时添加单独的数据框列...有什么想法吗?

boilerDf['Hour'] = boilerDf.DatetimeIndex.hour

亲切的问候

我在 Github 上也有数据上传: bbartling/Data on Github

【问题讨论】:

  • 您可以从您的文件中发布一些示例数据吗?如果没有您的数据或与之近似的示例数据,我们将无法运行您的任何代码。
  • 我添加了一个指向我创建的 Github 帐户的链接。它是相同的 CSV 文件

标签: python python-3.x pandas datetime datetimeindex


【解决方案1】:

我最初建议 .index.strftime() 作为这个答案。但是,Henry 还发现了 jezrael 的 Pandas time series data Index from a string to float,它返回整数类型的列。因此,我在这里包含了后者的扩展版本。使用这两种不同的方法时,输出会有微小的差异。

from numpy.random import randint
import pandas as pd

# Create a df with a date-time index with data every 6 hours
rng = pd.date_range('1/5/2018 00:00', periods=5, freq='6H')
df = pd.DataFrame({'Random_Number':randint(1, 10, 5)}, index=rng)

# Getting different time information in columns of type object
df['year'] = df.index.strftime('%Y')
df['month'] = df.index.strftime('%b')
df['date'] = df.index.strftime('%d')
df['hour'] = df.index.strftime('%H')
df['Day_of_week'] = df.index.strftime('%a')

# Getting different time information in columns of type integer
df['year'] = df.index.year
df['month'] = df.index.month
df['date'] = df.index.day
df['hour'] = df.index.hour
df['Day_of_week'] = df.index.dayofweek

df.head()
                     Random_Number  year month date hour Day_of_week
date                                                                
2018-01-05 00:00:00              8  2018   Jan   05   00         Fri
2018-01-05 06:00:00              8  2018   Jan   05   06         Fri
2018-01-05 12:00:00              1  2018   Jan   05   12         Fri
2018-01-05 18:00:00              4  2018   Jan   05   18         Fri
2018-01-06 00:00:00              7  2018   Jan   06   00         Sat

                     Random_Number  year  month  date  hour  Day_of_week
2018-01-05 00:00:00              3  2018      1     5     0            4
2018-01-05 06:00:00              1  2018      1     5     6            4
2018-01-05 12:00:00              9  2018      1     5    12            4
2018-01-05 18:00:00              5  2018      1     5    18            4
2018-01-06 00:00:00              8  2018      1     6     0            5

【讨论】:

  • 这是有效的...有没有办法让df['Day_of_week'] 成为星期几的数值? IE 星期日 = 0,星期一 = 1,星期二 = 3,等等...谢谢
  • df['month'] 也可以是数值吗?如果需要,我还将帖子编辑到我为 CSV 数据创建的 Github 帐户中。感谢您的宝贵时间。
  • 一个字符串到一个浮点数:)
  • 从 jezrael 获得输入做得很好。我已经修改了这个答案,以展示两种方法产生的输出的细微差别。
猜你喜欢
  • 2015-10-12
  • 2021-01-15
  • 2015-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-16
相关资源
最近更新 更多