Python DateTimeIndex.Series属性错误
现在,我的数据框有两列:DateTimeIndex和Load列。我想根据DateTimeIndex添加第三列,该列具有从零开始的连续第二个计数Python DateTimeIndex.Series属性错误,python,pandas,dataframe,datetimeindex,Python,Pandas,Dataframe,Datetimeindex,现在,我的数据框有两列: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
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
将熊猫作为pd导入
将matplotlib.pyplot作为plt导入
从scipy输入信号
将numpy作为np导入
#创建示例数据
df=pd.数据帧([[2020-07-2509:26:28',2],[2020-07-2509:26:29',10],[2020-07-2509:26:32',203],[2020-07-2509:26:33',30],
列=[“时间”,“加载])
df['Time']=pd.to_datetime(df['Time'])
df=df.设置索引(“时间”)
rng=pd.date_范围(测向索引[0],测向索引[-1],频率='s')
df=df.reindex(rng.fillna)(0)
##从DateTimeIndex创建已用秒数时间序列
ts=pd.系列(测向指数(范围(长度(测向指数)),指数=测向指数))
#期望输出
负载计数
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
#实际产量
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在里面
17##从DateTimeIndex创建已用秒数列
18
--->19 ts=pd.系列(测向指数(范围(长度(测向指数)),指数=测向指数))
20
21#df[“秒”]=
TypeError:“DatetimeIndex”对象不可调用
问题似乎在于指令
df.index(范围(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创建一个以秒为单位的连续时间列。这仍然不能告诉我你想要什么。在社区中,最好发布包含所需输出的内容,请查看此资源:谢谢你,Yuca。我会这样做。