1

How can I convert a "yyyy-MM-dd'T'HH:mm:ssZ'" format in a dataframe to a datetime format that I can further format to an index

2021-01-02T05:22:58.000Z is one of the dates in the dataframe

i've tried this line of code:

df['created_at_tweet']= pd.to_datetime(df['created_at_tweet'], format=("yyyy-MM-dd'T'HH :mm:ss.SSS'Z'"))

but i get the error

ValueError: time data '2021-01-02T01:43:32.000Z' does not match format 'yyyy-MM-dd'T'HH :mm:ss.SSS'Z'' (match)

any ideas?

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Lilsss
  • 13
  • 1
  • 3
  • You've got a space after `HH` for no obvious reason in your format string. Just get rid of the space. – Jon Skeet Jul 08 '22 at 17:28
  • Does this answer your question? [How do I parse an ISO 8601-formatted date?](https://stackoverflow.com/questions/127803/how-do-i-parse-an-iso-8601-formatted-date) – FObersteiner Jul 08 '22 at 17:45
  • That's just not a valid parsing directive in Python. And basically, you can use pd.to_datetime *without* providing a `format` here. – FObersteiner Jul 08 '22 at 17:46
  • I used pd.to_datetime and I don't get an error anymore. But the dates in the dataframe dont have a valid date format to create an index All dates in the created_at_tweet column are now: – Lilsss Jul 08 '22 at 17:58
  • Reload your data, then just `df['created_at_tweet']= pd.to_datetime(df['created_at_tweet'])` – FObersteiner Jul 08 '22 at 18:03
  • `df['created_at_tweet']= pd.to_datetime(df['created_at_tweet'])` gives me the error ` is not convertible to datetime` – Lilsss Jul 08 '22 at 18:12
  • Sorry but I don't see much point in more guessing, can you please provide a [mre]? – FObersteiner Jul 08 '22 at 18:15

1 Answers1

1

This works

df = pd.DataFrame({'created_at_tweet' : ['2021-01-02T01:43:32.000Z'], 'tweet' : ['Hello Twitter!']})

df['created_at_tweet']= pd.to_datetime(
  df['created_at_tweet'], 
  format=('%Y-%m-%dT%H:%M:%S.%f'))

yields

df

enter image description here

user3313834
  • 7,327
  • 12
  • 56
  • 99
7shoe
  • 1,438
  • 1
  • 8
  • 12