How to correctly use the "MMM" parse token in Arrow Python

Question:

I’ve got a number of datetimes in a spreadsheet that look like this:

July 29, 2022 @ 9:44 AM
Aug 2, 2022 @ 6:30 PM
...

I’m attempting to parse these with the following but I get an exception:

>>> import arrow
>>> myformat = "MMM D, YYYY @ H:MM A"
>>> arrow.get("Aug 2, 2022 @ 11:37 PM", myformat)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:UsersenricojrProjectspython-lox.venvlibsite-packagesarrowapi.py", line 91, in get
    return _factory.get(*args, **kwargs)
  File "C:UsersenricojrProjectspython-lox.venvlibsite-packagesarrowfactory.py", line 295, in get
    dt = parser.DateTimeParser(locale).parse(
  File "C:UsersenricojrProjectspython-lox.venvlibsite-packagesarrowparser.py", line 346, in parse
    return self._build_datetime(parts)
  File "C:UsersenricojrProjectspython-lox.venvlibsite-packagesarrowparser.py", line 701, in _build_datetime
    datetime(
ValueError: month must be in 1..12
>>> arrow.__version__
'1.2.2'
>>>

Documentation states that "MMM" is the token for abbreviated months, but it’s expecting a number from 1 – 12 instead. Am I using the tokens incorrectly, or is something else wrong here?

Python version: 3.10.4
Arrow version: 1.2.2
Operating System: Windows 10, and locale is reported by Python as en_US.

>>> import ctypes
>>> windll = ctypes.windll.kernel32
>>> windll.GetUserDefaultUILanguage()
1033

(1033 is en_US)

Asked By: Enrico Tuvera Jr

||

Answers:

I think the problem is your minutes in the format string:

myformat = "MMM D, YYYY @ H:MM A"

The minutes should should be mm, lowercase, the problem is there, not with MMM.

So when you do arrow.get("Aug 2, 2022 @ 11:37 PM", myformat), it’s complaining that 37 is outside of 1 and 12.

https://arrow.readthedocs.io/en/latest/index.html

  • Month token: MM 01, 02, 03 … 11, 12

  • Minute token: mm 00, 01, 02 … 58, 59

Also, going forward, beware ‘July’ and ‘Aug’ are different formats.

Answered By: 11574713
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.