【问题标题】:Pandas DateTime index resampling not workingPandas DateTime 索引重采样不起作用
【发布时间】:2020-01-12 19:46:41
【问题描述】:

我有一个 pandas 数据框,如下面的代码所示。我正在尝试“重新采样” 获取票列每日计数的数据。它没有给出任何错误,但 重新采样它不工作。这是一个更大数据集的样本。我想成为 能够按天、周、月季度等计算。但是 .resample 选项是 没有给我解决方案。我做错了什么?

import pandas as pd
df = pd.DataFrame([['2019-07-30T00:00:00','22:15:00','car'],
                    ['2013-10-12T00:00:00','0:10:00','bus'],
                    ['2014-03-31T00:00:00','9:06:00','ship'],
                    ['2014-03-31T00:00:00','8:15:00','ship'],
                    ['2014-03-31T00:00:00','12:06:00','ship'],
                    ['2014-03-31T00:00:00','9:24:00','ship'],
                    ['2013-10-12T00:00:00','9:06:00','ship'],
                    ['2018-03-31T00:00:00','9:06:00','ship']],
                    columns=['date_field','time_field','transportation'])
df['date_field2'] = pd.to_datetime(df['date_field'])
df['time_field2'] = pd.to_datetime(df['time_field'],unit = 'ns').dt.time
df['date_time_field'] = df.apply(lambda df : pd.datetime.combine(df['date_field2'],df['time_field2']),1)
df.set_index(['date_time_field'],inplace=True)
df.drop(columns=['date_field','time_field','date_field2','time_field2'],inplace=True)
df['tickets']=1
df.sort_index(inplace=True)
df.drop(columns=['transportation'],inplace=True)
df.resample('D').sum()
print('\ndaily resampling:')
print(df)

【问题讨论】:

    标签: python pandas time-series


    【解决方案1】:

    我认为您忘记将输出分配给变量,例如:

    df1 = df.resample('D').sum()
    print (df1)
    

    你的代码也应该简化:

    #join columns together with space and pop for extract column
    df['date_field'] = pd.to_datetime(df['date_field']+ ' ' + df.pop('time_field'))
    #create and sorting DatetimeIndex, remove column
    df = df.set_index(['date_field']).sort_index().drop(columns=['transportation'])
    #resample counts
    df1 = df.resample('D').size()
    print (df1)
    date_field
    2013-10-12    2
    2013-10-13    0
    2013-10-14    0
    2013-10-15    0
    2013-10-16    0
                 ..
    2019-07-26    0
    2019-07-27    0
    2019-07-28    0
    2019-07-29    0
    2019-07-30    1
    Freq: D, Length: 2118, dtype: int64
    

    另外我认为inplace 不是一个好习惯,请检查thisthis

    【讨论】:

    • 谢谢,这绝对是一个很好的解释,尤其是因为我似乎不必要地沿着原地路线走。
    猜你喜欢
    • 2016-09-06
    • 1970-01-01
    • 2016-09-20
    • 2021-12-14
    • 2013-06-09
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多