Python 日期时间索引重新采样不起作用

Python 日期时间索引重新采样不起作用,python,pandas,time-series,Python,Pandas,Time Series,我有一个熊猫数据框,如下面的代码所示。我正在尝试“重新采样” 用于获取票证列的每日计数的数据。它没有给出任何错误,但是 重新采样,而不是wokring。这是一个更大数据集的示例。我想成为 能够按天、周、月、季度等获取计数,但.resample选项不可用 没有给我一个解决方案。我做错了什么 import pandas as pd df = pd.DataFrame([['2019-07-30T00:00:00','22:15:00','car'], ['20

我有一个熊猫数据框,如下面的代码所示。我正在尝试“重新采样” 用于获取票证列的每日计数的数据。它没有给出任何错误,但是 重新采样,而不是wokring。这是一个更大数据集的示例。我想成为 能够按天、周、月、季度等获取计数,但.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)

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

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
不是一个好的做法,请检查并确认。

谢谢,这绝对是一个很好的解释,特别是因为我在不必要的情况下走了很多inplace路线。