2021年4月11日星期日

pandas datetime and extract year == '2021'

I successfully found DateTime from my csv data and extracted YEAR.

df_AnimalIns['DATETIME'] = pd.to_datetime(df_AnimalIns['DATETIME'])    df_year = df_AnimalIns['DATETIME'].dt.year    df_year    0    2020  1    2020  2    2020  3    2020  4    2021  5    2021  6    2021  7    2021  8    2021  Name: DATETIME, dtype: int64  

there's definitely data that's YEAR= 2021, BUT if you write the code as shown below,

df_AnimalIns['DATETIME'] = pd.to_datetime(df_AnimalIns['DATETIME'])    df_year = df_AnimalIns['DATETIME'].dt.year      df_AnimalIns[df_year == '2021']  

ANIMAL_ID ANIMAL_TYPE DATETIME

There is no data like this.

How can I fix this?

https://stackoverflow.com/questions/67052157/pandas-datetime-and-extract-year-2021 April 12, 2021 at 11:04AM

没有评论:

发表评论