Python Pandas DatetimeIndex.hour

亨利·哈布

我试图在我的数据帧中为3个单独的列构建带有DatetimeIndex的时间戳HOUR,DAY,MONTH的值。

我为无法复制的数据表示歉意,因为正在从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  

以下这段代码无法为日期时间索引的小时添加单独的dataframe列...有任何想法吗?

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

亲切的问候

我在Github上也上传了数据:在Github上的bbartling / Data

克尔科罗夫

我最初建议使用.index.strftime()来获得此答案。但是,亨利还找到了jezrael的Pandas时间序列数据索引,该索引从字符串浮点数返回整数类型的列。因此,我在这里包括了后者的扩展版本。使用两种不同的方法时,输出会有很小的差异。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python DatetimeIndex错误-TypeError :(“无法在<class'pandas.tseries.index.DatetimeIndex'

Python,Pandas:将DateTimeIndex与Period进行比较的布尔索引

pandas-用DateTimeIndex切片DataFrame的Python方式

Python熊猫DateTimeIndex

python中datetimeindex的长度

寻找Pandas.DateTimeIndex.is_dst()

Python Pandas:在缺少时间戳的情况下将DateTimeIndex拆分为两个

Python Pandas:是否可以在多索引数据框中将日期对象转换为DateTimeIndex?

如何按小时快速过滤 Pandas DatetimeIndex

Pandas Period/DateTimeIndex --> 年中的天数(包括闰年)

差异pandas.DateTimeIndex没有频率

用DatetimeIndex切片Pandas DataFrame

仅当创建MultiIndex时,Pandas DatetimeIndex NonExistentTimeError

Pandas:根据日期裁剪 DateTimeIndex 中的时间

如何切片pandas.DatetimeIndex?

将pandas DateTimeIndex转换为Unix时间?

如何使用 Pandas 在 datetimeindex 上进行连接?

将Pandas DatetimeIndex转换为数字格式

将 Pandas 索引设置为给定的 DateTimeindex

Python-pandas-Datetimeindex:分析步数滚动的最pythonic策略是什么?(例如每天某些时间)

在python pandas中的时间戳中合并字符串Date和int Hour

如何按位置选择 DatetimeIndex 的 Pandas 日期时间?

pandas.DatetimeIndex.snap时间戳记向左出现的频率

pandas.DatetimeIndex的频率为“无”,无法设置

使用DatetimeIndex重塑Pandas数据框以制作网格

对 DateTimeIndex 进行排序时 Pandas FutureWarning 的解决方法

如何根据 Pandas datetimeindex 划分我的“timeconsume”功能

如何在PySpark中将Pandas的DatetimeIndex转换为DataFrame?

pandas.DatetimeIndex可以记住它是否关闭吗?