ninesalt
ninesalt

Reputation: 4354

Pandas cannot call function on a datetimeindex

I'm trying to call a dayofweek() on my datetimeindex in a dataframe. Here is my code:

v = [
[
    1582041084.72,
    "1989121.03"
],
[
    1582041684.72,
    "1989121.03"
]


df = pd.DataFrame.from_records(v, columns=['time', 'points'])
df = df.astype({'points': 'float'})
df['time'] = pd.to_datetime(df['time'], unit='s')
df = df.set_index('time')
filtered = df.between_time('10:00', '14:30')

print(filtered.index.dayofweek()) 

This returns an error saying 'Int64Index' object is not callable.

Here's the output of df.index:

DatetimeIndex(['2020-02-19 10:01:24.720000029',
               '2020-02-19 10:11:24.720000029'],
              dtype='datetime64[ns]', name='time', freq=None)

Upvotes: 1

Views: 501

Answers (2)

filbranden
filbranden

Reputation: 8898

Note that dayofweek is a property and not a method.

That means you should use it without the parens:

print(filtered.index.dayofweek)

You can see that that's what the error message is telling you, 'Int64Index' object being the result of the property and it's complaining that you're trying to "call" it as a function, but it is not callable.

Upvotes: 2

Jim Eisenberg
Jim Eisenberg

Reputation: 1500

dayofweek doesn't need (). Simply do:

print(df.index.dayofweek)

Upvotes: 2

Related Questions