Home > Blockchain >  Separate date and time from csv column
Separate date and time from csv column

Time:12-10

I have a CSV file which has a column datetime in this format:

datetime dataA dataB
211201000000 1 222
211201000000 11 221
211201000000 12 111
211201000000 11 144
211201000000 13 1122

I want to write datetime column in this format (format='%Y-%m-%d %H:%M:%S'):

2021-12-01 00:00:00
2021-12-01 00:00:00
2021-12-01 00:00:00
2021-12-01 00:00:00
2021-12-01 00:00:00

I am using this line to change my datetime column:

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

or

df['datetime'] = pd.to_datetime(df['datetime']).dt.date

Output I get in datetime column from above lines are:

1970-01-01 }
.........
........

which are not even in CSV columns.

But these lines don't change my date time as I expect.

How can I change them in to my desire format so that I can use it easily in further work?

CodePudding user response:

The format you need to use is %y%m%d%H%M%S. A short example:

from datetime import datetime as dt

s = "211201000000"

dt.strptime(s, "%y%m%d%H%M%S")

OUTPUT

datetime.datetime(2021, 12, 1, 0, 0)

If you have already a DataFrame:

import pandas as pd

df = pd.DataFrame({"date":["211201000000", "211201000000"]})

df["date"] = pd.to_datetime(df["date"], format = "%y%m%d%H%M%S")

df

OUTPUT

        date
0 2021-12-01
1 2021-12-01

Then, you can format your date the way you prefer, for example:

df["date"].dt.strftime("%Y-%m-%d %H:%M:%S")

OUTPUT

0    2021-12-01 00:00:00
1    2021-12-01 00:00:00
  • Related