Home > other >  Changing data in a list into Datetime values
Changing data in a list into Datetime values

Time:01-14

I want to be able to change the following dates on this data set to this Datetime format YYYY-MM-DD-HH:

Date                
20220113T23Z   16877
20220113T22Z   15909
20220113T21Z   15204
20220113T20Z   15092
20220113T19Z   14804
...              ...
20150701T09Z   12387
20150701T08Z   12385
20150701T07Z   12445
20150701T06Z   12349
20150701T05Z   12583

however I haven't been able to do it successfully. If someone could let me know how I should be approaching this I would be very thankful

CodePudding user response:

I'm assuming that you are using pandas.

The answer depends on the type of the variable Date. You can check the type of your Serie with df["Date"].dtype

If Date is type Object (string), then you can create a new variable with the format you need with .str accesor.

df["Date"].str[0:4]   "-"   df["Date"].str[4:6]   "-"   df["Date"].str[6:8]   "-"   df["Date"].str[9:11]

If Date is type Datetime, then you can create a new variable with the format you need with .dt accesor and the function strftime()

df["Date"].dt.strftime("%Y-%m-%d-%H")

In both cases, the resulting Serie is type Object

  •  Tags:  
  • Related