首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >巨蟒熊猫DatetimeIndex.hour

巨蟒熊猫DatetimeIndex.hour
EN

Stack Overflow用户
提问于 2018-02-03 02:06:22
回答 1查看 7.7K关注 0票数 6

我正在尝试使用DatetimeIndex在我的数据框中为时间戳小时、日、月的值构建3个单独的列。

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

代码语言:javascript
复制
boilerDf = pd.read_csv('C:\\Users\\Python Scripts\\Deltadata.csv', index_col='Date', parse_dates=True)

print(boilerDf.info())

这将返回:

代码语言:javascript
复制
<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种方法:

代码语言:javascript
复制
DatetimeIndex.month 
DatetimeIndex.day   
DatetimeIndex.hour  

下面的代码不能为日期时间索引的小时添加单独的dataframe列...有什么想法吗?

代码语言:javascript
复制
boilerDf['Hour'] = boilerDf.DatetimeIndex.hour

亲切的问候

我还将数据上传到Github:bbartling/Data on Github

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-04 11:29:07

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

代码语言:javascript
复制
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
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48588498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档