convert a pandas column of string dates to compare with datetime.date

Viewed 32

I have a column of string values in pandas as follows: 2022-07-01 00:00:00+00:00

I want to compare it to a couple of dates as follows:

month_start_date = datetime.date(start_year, start_month, 1)
month_end_date = datetime.date(start_year, start_month, calendar.monthrange(start_year, start_month)[1])
df = df[(df[date] >= month_start_date) and (df[date] <= month_end_date)]

How do i convert the string value to datetime.date?

I have tried to use pd.to_datetime(df['date']), says cant compare datetime to date
Tried to use pd.to_datetime(df['date']).dt.date says dt can only be used for datetime l like variables, did you mean at

Also tired to normalize it, but that bring more errors with timezone, and active and naive timezone

Also tried .astype('datetime64[ns]')

None of it is working

UPDATE

Turns out none of the above are working because half the data is in this format: 2022-07-01 00:00:00+00:00

And the rest is in this format: 2022-07-01

Here is how i am getting around this issue:

for index, row in df_uscis.iterrows():
    df_uscis.loc[index, 'date'] = datetime.datetime.strptime(row['date'].split(' ')[0], "%Y-%m-%d").date()

Is there a simpler and faster way of doing this? I tried to make a new column with the date values only, but not sure how to do that

1 Answers

From your update, if you only need to turn the values from string to date objects, you can try:

df['date'] = pd.to_datetime(df['date'].str.split(' ').str[0])
df['date'] = df['date'].dt.date

Also, try to avoid using iterrows, as it is really slow and usually there's a better way to achieve what you're trying to acomplish, but if you really need to iterate through a DataFrame, try using the df.itertuples() method.

Related