0

I am trying to convert a DataFrame to datetime format using pd.datetime() but then if I try to convert the attribute further I get the following error:

AttributeError: Can only use .dt accessor with datetime like values

this is my script:

exms_data = pd.read_csv(r"C:\Users\MandijaE\Downloads\pdwh[VE249977].csv", sep=";")``` 
    
exms_data['CHANGE_TIME.1'] = pd.to_datetime(exms_data['CHANGE_TIME.1'], format='%d.%m.%y %H:%M:%S,%f %z')
    
exms_data['CHANGE_TIME.1'] = exms_data['CHANGE_TIME.1'].dt.tz_convert('UTC')
3
  • 1
    We need to see how your dataframe looks like Commented Aug 25, 2023 at 7:25
  • this is oen record for example: 2022-09-06 08:43:37.139000+02:00 Commented Aug 25, 2023 at 7:26
  • you seem to have mixed UTC offset. pandas will parse that to vanilla Python datetime, not pandas datetime. Use pd.to_datetime(exms_data['CHANGE_TIME.1'], format='%d.%m.%y %H:%M:%S,%f %z', utc=True) to avoid the issue. Commented Aug 25, 2023 at 9:47

1 Answer 1

0

Make sure that the CHANGE_TIME.1 column in your CSV file indeed contains values in the format 2022-09-06 08:43:37.139000+02:00. See if it helps:

# Convert 'CHANGE_TIME.1' column to datetime with timezone information
exms_data['CHANGE_TIME.1'] = pd.to_datetime(exms_data['CHANGE_TIME.1'], format='%Y-%m-%d %H:%M:%S.%f%z')

# Convert timezone to UTC
exms_data['CHANGE_TIME.1'] = exms_data['CHANGE_TIME.1'].dt.tz_convert('UTC')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.