Python Forum
Simple String to Time within a pandas dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple String to Time within a pandas dataframe
#1
Hi

I have a data frame with strings that look like date and another column of time.
What would be the best way to convert them to actual datetime objects (or to two objects of date and time) ?

Note : After looking online, it seems I should be able to use the datetime.time.strptime function, however it doesnt appear to be a proper method/function (ie not listed). strftime i...

Looking to update the column (data imported from csv text file via pandas.read_csv)

Point to something that works would be great...

Data frame looks like :
date atime other columns
13-06-2019 10:00 blah blah
12-06-2019 09:15 blah blah blah

Thanks in advance
Reply
#2
You can use pd.to_datetime utility function, e.g.
import pandas as pd
df = pd.DataFrame({'atime': ['13-06-2019 10:00', '12-06-2019 09:15'], 'x': [1, 2]})
df.atime = pd.to_datetime(df.atime)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Parsing and summing time deltas (duration) onto bar + pie charts using pandas - - DRY Drone4four 2 514 Feb-10-2024, 06:04 PM
Last Post: Drone4four
  HTML Decoder pandas dataframe column mbrown009 3 962 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Pandas read csv file in 'date/time' chunks MorganSamage 4 1,647 Feb-13-2023, 11:24 AM
Last Post: MorganSamage
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,091 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 1,396 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,269 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,243 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,758 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,527 Jan-10-2022, 04:42 PM
Last Post: moduki1
  Simple pandas question mcva 4 2,607 Dec-17-2021, 04:47 PM
Last Post: mcva

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020