Home > Software design >  Combine date and time column in pandas
Combine date and time column in pandas

Time:03-09

Good morning,

I have a question regarding combining date and time columns into a single column in pandas.

The data types of my columns are as follows:

Date column: ytdsorted["Checkin\nDate"].dtype output: dtype('<M8[ns]')

Time column: ytdsorted["Checkin\nTime"].dtype output: dtype('O')

A preview of how my data looks is as follows:

Date column: ytdsorted["Checkin\nDate"].head(1) output: 2021-10-01

Time column: ytdsorted["Checkin\nTime"].head(1) output: 12:42 PM

I would like to combine the date and time column to give me a 24hr format: "yyyy-mm-dd hh:mm"

Any help would be much appreciated! Cheers

CodePudding user response:

df = pd.DataFrame({
    "Date":["2021-10-01"],
    "Time":["12:42 PM"]
})
df["dateMerge"] = pd.to_datetime(df["Date"] " " df["Time"], infer_datetime_format=True)

Date            Time             dateMerge
2021-10-01      12:42 PM         2021-10-01 12:42:00
  • Related