I have a date column that's of string/object type:
Day - 2022 Day - 2021 ...
0 01/01/2022 01/01/2021 ...
1 02/01/2022 02/01/2021 ...
2 03/01/2022 03/01/2021 ...
3 04/01/2022 04/01/2021 ...
4 05/01/2022 05/01/2021 ...
.. ... ... ...
725 27/12/2023 NaN ...
726 28/12/2023 NaN ...
727 29/12/2023 NaN ...
728 30/12/2023 NaN ...
729 31/12/2023 NaN ...
I can cast to date no problem like so:
pd.to_datetime(df["Day - 2022"])
0 2022-01-01
1 2022-02-01
2 2022-03-01
3 2022-04-01
4 2022-05-01
...
725 2023-12-27
726 2023-12-28
727 2023-12-29
728 2023-12-30
729 2023-12-31
Name: Day - 2022, Length: 730, dtype: datetime64[ns]
But when I use np.where
to detect whether a string column is in fact a date in string format it returns big integer instead:
col = "Day - 2022"
pattern = "^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{2,4}"
df[col] = np.where(
df[col].str.match(pattern),
pd.to_datetime(df[col]),
df[col],
)
Day - 2022 Day - 2021 ...
0 1640995200000000000 01/01/2021 ...
1 1643673600000000000 02/01/2021 ...
2 1646092800000000000 03/01/2021 ...
3 1648771200000000000 04/01/2021 ...
4 1651363200000000000 05/01/2021 ...
.. ... ... ...
725 1703635200000000000 NaN ...
726 1703721600000000000 NaN ...
727 1703808000000000000 NaN ...
728 1703894400000000000 NaN ...
729 1703980800000000000 NaN ...
Confused as to why this is happening? Any ideas on how to prevent this from happening?