Bera
Bera

Reputation: 1949

Select by month

I have a dateframe with a date field called datum

df['datum'].iloc[0]
Out[58]: datetime.datetime(2016, 5, 2, 2, 0, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=120, name=None))

type(df['datum'].iloc[0])
Out[59]: datetime.datetime

I want to select rows with a month <= 6:

df.loc[df['datum'].dt.month <= 6]
AttributeError: Can only use .dt accessor with datetimelike values

But all rows are datetimes?

df['datum'] = df['datum'].apply(pd.to_datetime, errors='raise')

no error.

Upvotes: 1

Views: 49

Answers (1)

jezrael
jezrael

Reputation: 862641

It seems there are object datetimes, so for converting to Timestamps need:

print (df['datum'].dtype)
object

df['datum'] = pd.to_datetime(df['datum'], utc=True)

df.loc[df['datum'].dt.month <= 6]

Upvotes: 2

Related Questions