Skip to content

[C++] strptime rolls-over dates not in range for current month  #31374

Open

Description

@asfimport

I noticed some potentially unexpected behaviour when converting from string to date. Days that are out of bounds for the given month are rolled over into the following month.

I think the expected behaviour would be to either error (Python) or return NULL/NA (R), but not to roll over dates in the following month.

library(arrow, warn.conflicts = FALSE)
library(lubridate, warn.conflicts = FALSE)
library(dplyr, warn.conflicts = FALSE)

df <- tibble::tibble(string_date = "1999-02-30")

# base R returns NA
df %>% 
  mutate(date = strptime(string_date, format = "%Y-%m-%d"))
#> # A tibble: 1 × 2
#>   string_date date  
#>   <chr>       <dttm>
#> 1 1999-02-30  NA

# arrow rolls over the 30th of February into the 2nd of March
df %>% 
  arrow_table() %>% 
  mutate(date = strptime(string_date, format = "%Y-%m-%d")) %>% 
  collect()
#> # A tibble: 1 × 2
#>   string_date date               
#>   <chr>       <dttm>             
#> 1 1999-02-30  1999-03-02 00:00:00

Thanks Alenka, Joris and Rok for helping me with the Python examples:
pandas:

>>> import pandas as pd
>>> pd.to_datetime("1999-02-30", format="%Y-%m-%d")
...
ValueError: time data 1999-02-30 doesn't match format specified

datetime:

>>> import datetime
>>> from datetime import datetime
>>> datetime.strptime("1999-02-30", "%Y-%m-%d")
...
ValueError: day is out of range for month

arrow:

>>> import pyarrow.compute as pc
>>> print(pc.strptime("1999-02-30", format="%Y-%m-%d", unit="s"))
1999-03-02 00:00:00

Reporter: Dragoș Moldovan-Grünfeld / @dragosmg
Watchers: Rok Mihevc / @rok

Related issues:

Note: This issue was originally created as ARROW-15948. Please see the migration documentation for further details.

Activity

  1. asfimport commented on Mar 16, 2022

    @asfimport
    Author

    Joris Van den Bossche / @jorisvandenbossche:
    Copying my comment from the other issue: personally, I don't like that behaviour, but I suppose we get this from the system strptime? (so that might even depend on your OS?)
    It might be interesting to check what date.h's version of strptime does.

    I think the expected behaviour would be to either error (Python) or return NULL/NA (R),

    I would say it is both: by default error, and optionally return null (after ARROW-15665)

  2. asfimport commented on Mar 16, 2022

    @asfimport
    Author

    Rok Mihevc / @rok:
    Two notes:

    Perhaps system strptimes have an option to disable this behaviour?

    Switching from system to date.h strptime might have performance implications so we should probably benchmark as we change this. 

  3. asfimport commented on Jun 29, 2022

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    Does date.h have a strptime? We use a vendored implementation from musl on Windows.

  4. asfimport commented on Jun 29, 2022

    @asfimport
    Author
  5. asfimport commented on Jun 29, 2022

    @asfimport
    Author

    Antoine Pitrou / @pitrou:
    Ah, well, let's not use it, then :)

  6. asfimport commented on Jun 29, 2022

    @asfimport
    Author

    Rok Mihevc / @rok:
    But maybe it's nice to dates? :D

  7. thisisnic commented on Jul 30, 2026

    @thisisnic
    Member

    Still an issue as of 25.0.0 - potential fix is to call the .ok() method to check.

  8. added
    Status: needs championHigh impact issues which aren't being worked on but require a volunteer to move the task forward.
    on Jul 30, 2026
  9. added 2 commits that reference this issue on Jul 30, 2026
  10. added a commit that references this issue on Sep 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions