简体   繁体   English

以小时间隔填写获得的日期分类? Pandas Python

[英]Fill the obtained day classification in its hour interval? Pandas Python

I am trying to fill an hour interval DataFrame with a whole Day classification, you can copy/paste the code, it should run:我正在尝试用一整天的分类填充一个小时间隔 DataFrame,您可以复制/粘贴代码,它应该运行:

import pandas as pd
from datetime import timedelta, date

column2 = [1, 2, 3, 4, 7, 8, 9, 10]
column1 = [item for item in range(1, 74)]
column3 = pd.date_range('1998-01-01 00:00', freq='h', periods=73, tz ='Etc/GMT+0' )
column4 = ['1998-01-01 00:00:00', '1998-01-01 01:00:00', '1998-01-01 02:00:00', '1998-01-01 03:00:00 ', 
          '1998-01-01 06:00:00', '1998-01-01 07:00:00', '1998-01-01 08:00:00', '1998-01-01 09:00:00']
column5 = ['1998-01-01', '1998-01-02', '1998-01-03']
column6 = ['Overcast', 'Clear', 'High']

dtst_1 = pd.DataFrame()
dtst_1['column1'] = column1
dtst_1.set_index(column3, inplace=True)

dtst_2 = pd.DataFrame()
dtst_2['column2'] = column2
dtst_2['column4'] = column4
dtst_2['column4'] = pd.to_datetime(dtst_2['column4'])
dtst_2.set_index('column4', inplace=True)

dtst_3 = pd.DataFrame()
dtst_3['column6'] = column6
dtst_3['column5'] = column5
dtst_3['column5'] = pd.to_datetime(dtst_3['column5'])
dtst_3.set_index('column5', inplace=True)


dtst_2.index = pd.to_datetime(dtst_2.index).tz_localize('Etc/GMT+0')
dtst_3.index = pd.to_datetime(dtst_3.index).tz_localize('Etc/GMT+0')
dtst_2 = dtst_2.merge(dtst_1['colum1'], how = 'right', left_index=True, right_index=True)

def daterange_tst(start_date_tst, end_date_tst):
    for n in range(int ((end_date_tst - start_date_tst).days)):
        yield start_date_tst + timedelta(n)

start_date_tst = date(1998, 1, 1)
end_date_tst = date(1998, 1, 2)

for single_date_tst in daterange_tst(start_date_tst, end_date_tst):
    print(single_date_tst)
    dtst_2 = dtst_2.join(dtst_3['column6'], how = 'outer')

dtst_2.head(49)

And you should see this result:你应该看到这个结果:

dataframe dataframe

Is there any way to fill the NaN gaps in colum6 with the day classification?有没有办法用日分类来填补colum6中的NaN空白? (day 1 fill with Overcast, day 2 fill with Clear... etc...? Assuming, of course, that this is just a small section of a huge dataset, so is there any way to insert the classified day into the intra-hour range of that day? Thank you so much. (第 1 天用阴天填充,第 2 天用晴天填充......等等......?当然,假设这只是一个巨大数据集的一小部分,那么有没有办法将分类日期插入内部-那天的小时范围?非常感谢。

Is this what you are trying to do?这是你想要做的吗?

dtst_2['column6'].ffill(inplace=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM