Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine one Pandas dataframe column with a date and another with a time?

I'm working with a CSV that has two columns, one with a date in the month/day/year format (for example 01/15/2019) and another with a time in the format with a 12-hour time an "a" for AM and "p" for PM. Examples: 10/9/2017,9:50a and 10/9/2017,3:50p. I'd like to combine these two columns into one column of DateTime objects, for example "2017-10-09 09:50:00" and "2017-10-09 15:50:00".

These happen to be in columns 2 and 3 of the CSV. I've tried the following options with pd.read_csv:

  • Passing parse_dates = True: Nothing appears to happen. The resulting date and time columns are just "objects" according to dtypes, not DateTime objects.
  • Passing parse_dates = [[2,3]]: Combines the columns but doesn't result in a DateTime object.
  • Passing parse_dates = [2,3]: Converts the individual columns to DateTime objects but doesn't combine; the date column is correctly converted, but the time column ends up with today's date attached, e.g. 2019-01-15 15:50:00.

I've also tried date_parser but I am not sure what parsing function I need to give it -- and it just seems like pandas should be able to handle these date/time formats without coercing.

Any thoughts? Thanks in advance.

like image 634
Robert Talbert Avatar asked Dec 03 '25 15:12

Robert Talbert


1 Answers

You can convert to datetime after you have read your data into a dataframe. Since your date and time components are split across 2 series, trying to parse directly with pd.read_csv may be difficult.

from io import StringIO

x = """date,time
10/9/2017,9:50a
10/9/2017,3:50p"""

# replace StringIO(x) with 'file.csv'
df = pd.read_csv(StringIO(x))

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

print(df)
#         date   time            datetime
# 0  10/9/2017  9:50a 2017-10-09 09:50:00
# 1  10/9/2017  3:50p 2017-10-09 15:50:00
like image 165
jpp Avatar answered Dec 05 '25 03:12

jpp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!