Home > Enterprise >  pandas.to_datetime() does not filter when used with loc[] and comparison operator
pandas.to_datetime() does not filter when used with loc[] and comparison operator

Time:10-13

I downloaded a .csv file to do some practice, a column named "year_month" is string with the format "YYYY-MM"

By doing:

df = pd.read_csv('C:/..../migration_flows.csv',parse_dates=["year_month"])

"year_month" is Dtype=object. So far so good.

By doing:

df["year_month"] = pd.to_datetime(df["year_month"],format='%Y-%m-%d')

it is converted to daterime64[ns]. So far so good.

I try to filter certain dates by doing:

filtered_df = df.loc[(df["year_month"]>= pd.Timestamp(2018-1-1))]

The program returns the whole column as if nothing happened. For instance, it starts displaying, starting from the date "2001-01-01"

Any thoughts on how to filter properly? Many thanks

CodePudding user response:

how about this


df.loc[(df["year_month"]>= pd.to_datetime('2018-01-01'))]

or

df.loc[(df["year_month"]>= pd.Timestamp('2018-01-01'))]
  • Related