Timedelta parse AM/PM
See original GitHub issueI have some data that is roughly like “%H:%M:%S %P” (where %P is AM/PM). I’d like to store it as a Timedelta
representing seconds since midnight.
Currently parsing that “succeeds”, ignoring the AM / PM
In [3]: import pandas as pd
In [6]: raw = ['3:25:00 AM', '3:25:00 PM', '12:30:00 AM', '12:30:00 PM']
...: base = pd.to_timedelta(raw)
...: base
...:
Out[6]: TimedeltaIndex(['03:25:00', '03:25:00', '12:30:00', '12:30:00'], dtype='timedelta64[ns]', freq=None)
Here’s my current workaround.
In [7]: split = pd.Series(raw).str.extract(r"(?P<base>\S+) (?P<am_pm>\w{2})", expand=True)
...: base = pd.to_timedelta(split['base'])
...: hour = base.dt.total_seconds() // (60 * 60)
...:
...: move_ahead = (split.am_pm == "PM") & (hour < 12)
...: move_back = (split.am_pm == "AM") & (hour == 12)
...:
...: base[move_ahead] += pd.Timedelta("12H")
...: base[move_back] -= pd.Timedelta("12H")
...: base
...:
Out[7]:
0 03:25:00
1 15:25:00
2 00:30:00
3 12:30:00
Name: base, dtype: timedelta64[ns]
That works, but is a bit tricky to get right (assuming I have gotten it right). I’d propose that we either
- raise when we see am/pm in the data, and add this as cookbook recipe
- Try to support parsing this kind of data directly
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (7 by maintainers)
Top Results From Across the Web
Converting time to am/pm using python - Stack Overflow
datetime.timedelta represents a duration, not a time of day. ... time = datetime.datetime(2000, 1, 1) + datetime.timedelta(seconds ...
Read more >datetime — Basic date and time types — Python 3.11.1 ...
Third-party library with expanded time zone and parsing support. ... object timedelta tzinfo timezone time date datetime ... AM, PM (en_US);. am, pm...
Read more >Python Timedelta [Complete Guide] - PYnative
The Timedelta class available in Python's datetime module. Use the timedelta to add or subtract weeks, days, hours, minutes, seconds, ...
Read more >Python DateTime, TimeDelta, Strftime(Format) with Examples
Timedelta in Python is an object that represents the duration. It is mainly used to calculate the duration between two dates and times....
Read more >parser — dateutil 2.8.2 documentation - Read the Docs
This module offers a generic date/time string parser which is able to parse most known formats to represent a date and/or time. This...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The
'today'
part of this is a hack, but here’s a slightly simpler recipeYep!