Python:用日期时间重置 Pandas.Series 的索引

Python: Reset index of Pandas.Series with datetime

我正在尝试使用来自 statsmodels 库的 "seasonal_decompose" 方法:

decomposition = seasonal_decompose(ts_load_log)

尝试执行该行时出现以下错误:

'RangeIndex'对象没有属性'inferred_freq'

那是因为我的 ts_load_log 如下所示:

现在我正在尝试转换我的索引!因此,我创建了以下数据框,其中包含 ts_load_log:

中值的日期
df_datetimes  = pd.to_datetime(df_load_per_hour['Market Day']) + pd.to_timedelta(df_load_per_hour['HourEnding'] - 1, unit='h')

看起来像:

有没有办法在数据框 df_datetimes 的帮助下重置 ts_load_log 系列的索引,以便分解有效?或者有没有更简单的方法而不是创建新的数据框?还是我完全错了?

我已经试过了:

test = ts_load_log.to_frame()
test2 = df_datetimes.to_frame()
test = test.reindex(test2[0])

但我认为没有必要。另外它不起作用!

如果您使用 pandas 的 datetimeIndex 方法,它应该可以工作:

test = ts_load_log.to_frame()
test2 = df_datetimes.to_frame()
test.index = pd.DatetimeIndex(test2[0])

之后只需将数据框对象转换为 Series 对象,例如:

test = pd.Series(test)