AHalperin
AHalperin

Reputation: 49

DateTimeIndex Pandas .Series attribute Error

Right now, my data frame has two columns: a DateTimeIndex and a Load column. I want to add a third column with a consecutive second count, from zero, based on the 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

Upvotes: 0

Views: 680

Answers (2)

AHalperin
AHalperin

Reputation: 49

In case anyone else is asking a similar question to mine in a similarly confusing way (sorry, longtime users; I am still learning to ask questions better), here is the code that elegantly does what I want.

# 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

Upvotes: 0

Yuca
Yuca

Reputation: 6091

seems like the issue is the instruction

df.index(range(len(df.index))

you're using df.index() and that might be raising the not callable error (simple way to look at it: parenthesis are for methods, brackets are for indexing). If you want to use a slice of df.index use the syntax df.index[]. Since it is not clear what you want to achieve I can't recommend a better solution

UPDATE:

after looking at your desired output, you can achieve that by doing

df.asfreq('s').fillna(0)

Output:

                      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

And regarding the seconds, there might be a simpler way, but this is what I have for you:

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

Upvotes: 1

Related Questions