2 回答

TA貢獻(xiàn)1783條經(jīng)驗(yàn) 獲得超4個(gè)贊
將日期時(shí)間設(shè)置為索引(在轉(zhuǎn)換為日期時(shí)間之后),并使用帶有前向填充的 asfreq 方法用以前的值填充空值:
#thanks to @a_guest for the cleaned sample data
df = pd.DataFrame(
data=[['2016-02-24 01:00', 12, 15],
['2016-02-24 02:00', 14, 13],
['2016-02-24 03:00', 8, 12]],
columns=['Datetime', 'Level1', 'Level2']
)
df['Datetime'] = pd.to_datetime(df['Datetime'])
df = df.set_index('Datetime')
df.asfreq('15min',method='pad')
Level1 Level2
Datetime
2016-02-24 01:00:00 12 15
2016-02-24 01:15:00 12 15
2016-02-24 01:30:00 12 15
2016-02-24 01:45:00 12 15
2016-02-24 02:00:00 14 13
2016-02-24 02:15:00 14 13
2016-02-24 02:30:00 14 13
2016-02-24 02:45:00 14 13
2016-02-24 03:00:00 8 12
注意:您的問(wèn)題是時(shí)間間隔為 15 分鐘,您的最晚時(shí)間是下午 3 點(diǎn),而不是數(shù)據(jù)中共享的下午 4 點(diǎn)。

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以創(chuàng)建原始數(shù)據(jù)框的副本,其中您Datetime
每次將列遞增 15 分鐘,然后使用pd.concat
followed bysort_values
獲得最終結(jié)果:
from datetime import timedelta
import pandas as pd
df = pd.DataFrame(
data=[['2016-02-24 01:00', 12, 15],
['2016-02-24 02:00', 14, 13],
['2016-02-24 03:00', 8, 12]],
columns=['Datetime', 'Level1', 'Level2']
)
df['Datetime'] = pd.to_datetime(df['Datetime'])
intervals = [df]
while len(intervals) < 4:
new = intervals[-1].copy()
new['Datetime'] += timedelta(minutes=15)
intervals.append(new)
result = pd.concat(intervals).sort_values(by='Datetime')
添加回答
舉報(bào)