Home > Mobile >  Select by month
Select by month

Time:10-02

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.

CodePudding user response:

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]
  • Related