question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Timedelta parse AM/PM

See original GitHub issue

I 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

  1. raise when we see am/pm in the data, and add this as cookbook recipe
  2. Try to support parsing this kind of data directly

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
chris-b1commented, Dec 15, 2017

The 'today' part of this is a hack, but here’s a slightly simpler recipe


In [23]: raw = ['3:25:00 AM', '3:25:00 PM', '12:30:00 AM', '12:30:00 PM']

In [24]: pd.to_datetime(raw)
Out[24]: 
DatetimeIndex(['2017-12-15 03:25:00', '2017-12-15 15:25:00',
               '2017-12-15 00:30:00', '2017-12-15 12:30:00'],
              dtype='datetime64[ns]', freq=None)

In [25]: pd.to_datetime(raw) - pd.Timestamp('today').normalize()
Out[25]: TimedeltaIndex(['03:25:00', '15:25:00', '00:30:00', '12:30:00'], dtype='timedelta64[ns]', freq=None)
0reactions
WillAydcommented, Jun 26, 2019

Yep!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found