Skip to content Skip to sidebar Skip to footer

Combining Two Different Formats Of Datetime In Pandas

I have many csv files that contain date and time information. Problem is that I have two different formats of date. MM/DD/YYYY HH:MM:SS and MM-DD-YYYY HH:MM:SS I do not want to

Solution 1:

Use pandas.to_datetime before you merge them into one DataFrame/Series.

Solution 2:

Pandas to_datetime is pretty versatile, it will understand many different formats.

from io import StringIO

d_csv = StringIO("""12/01/2016 01:01:00
12-01-2016 02:02:00""")
d = pd.read_csv(d_csv, header=None)    

d[0] = pd.to_datetime(d[0])

print(d)

Output:

002016-12-01 01:01:0012016-12-01 02:02:00

Solution 3:

Try this, (with a function to parse date format)

import pandas as pd

defmyparser(x):
    return datetime.strptime(x, '%m/%d/%Y %H:%M:%S' )

df = pd.read_csv(filename,  parse_dates=True, date_parser=myparser)

Post a Comment for "Combining Two Different Formats Of Datetime In Pandas"