DateTimeIndex Pandas .Series 属性错误
DateTimeIndex Pandas .Series attribute Error
现在,我的数据框有两列:DateTimeIndex 和 Load 列。我想根据 DateTimeIndex 从零开始添加具有连续秒数的第三列。
import pandas as pd
import matplotlib.pyplot as plt
from scipy import signal
import numpy as np
# Create sample Data
df = pd.DataFrame([['2020-07-25 09:26:28',2],['2020-07-25 09:26:29',10],['2020-07-25 09:26:32',203],['2020-07-25 09:26:33',30]],
columns = ['Time','Load'])
df['Time'] = pd.to_datetime(df['Time'])
df = df.set_index("Time")
rng = pd.date_range(df.index[0], df.index[-1], freq='s')
df = df.reindex(rng).fillna(0)
## Create Elapsed Seconds Timeseries from DateTimeIndex
ts = pd.Series(df.index(range(len(df.index)), index=df.index))
# Desired Output
Load CountS
2020-07-25 09:26:28 2.0 1
2020-07-25 09:26:29 10.0 2
2020-07-25 09:26:30 0.0 3
2020-07-25 09:26:31 0.0 4
2020-07-25 09:26:32 203.0 5
2020-07-25 09:26:33 30.0 6
# Actual Output
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-32-02bfe0dcc12d> in <module>
17 ## Create Elapsed Seconds Column from DateTimeIndex
18
---> 19 ts = pd.Series(df.index(range(len(df.index)), index=df.index))
20
21 # df["Seconds"] =
TypeError: 'DatetimeIndex' object is not callable
似乎问题出在指令上
df.index(range(len(df.index))
您正在使用 df.index()
,这可能会引发不可调用错误(查看它的简单方法:括号用于方法,方括号用于索引)。如果要使用 df.index 的一部分,请使用语法 df.index[]
。由于不清楚你想要实现什么我不能推荐更好的解决方案
更新:
查看您想要的输出后,您可以通过
实现
df.asfreq('s').fillna(0)
输出:
Load
Time
2020-07-25 09:26:28 2.0
2020-07-25 09:26:29 10.0
2020-07-25 09:26:30 0.0
2020-07-25 09:26:31 0.0
2020-07-25 09:26:32 203.0
2020-07-25 09:26:33 30.0
关于秒数,可能有更简单的方法,但这就是我为您准备的:
df['CountS'] = df.index.to_series().diff().astype('timedelta64[s]').fillna(0).cumsum() + 1
Load CountS
Time
2020-07-25 09:26:28 2.0 1.0
2020-07-25 09:26:29 10.0 2.0
2020-07-25 09:26:30 0.0 3.0
2020-07-25 09:26:31 0.0 4.0
2020-07-25 09:26:32 203.0 5.0
2020-07-25 09:26:33 30.0 6.0
万一其他人以同样令人困惑的方式问我的类似问题(抱歉,长期用户;我仍在学习更好地提问),这里是优雅地做我想要的代码。
# Change datetimeindex to timedelta by subtracting to datetimeindices.
# Change to integers by appending .seconds to datetime
# Assign values to new column "count"
df["Count"] = (df.index - df_index[0]).seconds
现在,我的数据框有两列:DateTimeIndex 和 Load 列。我想根据 DateTimeIndex 从零开始添加具有连续秒数的第三列。
import pandas as pd
import matplotlib.pyplot as plt
from scipy import signal
import numpy as np
# Create sample Data
df = pd.DataFrame([['2020-07-25 09:26:28',2],['2020-07-25 09:26:29',10],['2020-07-25 09:26:32',203],['2020-07-25 09:26:33',30]],
columns = ['Time','Load'])
df['Time'] = pd.to_datetime(df['Time'])
df = df.set_index("Time")
rng = pd.date_range(df.index[0], df.index[-1], freq='s')
df = df.reindex(rng).fillna(0)
## Create Elapsed Seconds Timeseries from DateTimeIndex
ts = pd.Series(df.index(range(len(df.index)), index=df.index))
# Desired Output
Load CountS
2020-07-25 09:26:28 2.0 1
2020-07-25 09:26:29 10.0 2
2020-07-25 09:26:30 0.0 3
2020-07-25 09:26:31 0.0 4
2020-07-25 09:26:32 203.0 5
2020-07-25 09:26:33 30.0 6
# Actual Output
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-32-02bfe0dcc12d> in <module>
17 ## Create Elapsed Seconds Column from DateTimeIndex
18
---> 19 ts = pd.Series(df.index(range(len(df.index)), index=df.index))
20
21 # df["Seconds"] =
TypeError: 'DatetimeIndex' object is not callable
似乎问题出在指令上
df.index(range(len(df.index))
您正在使用 df.index()
,这可能会引发不可调用错误(查看它的简单方法:括号用于方法,方括号用于索引)。如果要使用 df.index 的一部分,请使用语法 df.index[]
。由于不清楚你想要实现什么我不能推荐更好的解决方案
更新:
查看您想要的输出后,您可以通过
实现df.asfreq('s').fillna(0)
输出:
Load
Time
2020-07-25 09:26:28 2.0
2020-07-25 09:26:29 10.0
2020-07-25 09:26:30 0.0
2020-07-25 09:26:31 0.0
2020-07-25 09:26:32 203.0
2020-07-25 09:26:33 30.0
关于秒数,可能有更简单的方法,但这就是我为您准备的:
df['CountS'] = df.index.to_series().diff().astype('timedelta64[s]').fillna(0).cumsum() + 1
Load CountS
Time
2020-07-25 09:26:28 2.0 1.0
2020-07-25 09:26:29 10.0 2.0
2020-07-25 09:26:30 0.0 3.0
2020-07-25 09:26:31 0.0 4.0
2020-07-25 09:26:32 203.0 5.0
2020-07-25 09:26:33 30.0 6.0
万一其他人以同样令人困惑的方式问我的类似问题(抱歉,长期用户;我仍在学习更好地提问),这里是优雅地做我想要的代码。
# Change datetimeindex to timedelta by subtracting to datetimeindices.
# Change to integers by appending .seconds to datetime
# Assign values to new column "count"
df["Count"] = (df.index - df_index[0]).seconds