Skip to content

BUG: misleading message when using to_datetime with format='%V %a' #50309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

MarcoGorelli
Copy link
Member

@MarcoGorelli MarcoGorelli commented Dec 17, 2022

Good call @mroeschke to factor this out! I think it's simpler like this, and it helped find some untested code, and other code where the message was misleading


Performance is unaffected:

On this branch:

In [1]: format = '%Y-%d-%m %H:%M:%S%z'

In [2]: dates = pd.date_range('1900', '2000').tz_localize('+01:00').strftime(format).tolist()

In [3]: %%timeit
   ...: pd.to_datetime(dates, format=format)
   ...: 
   ...: 
234 ms ± 470 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)

upstream/main (after having recompiled):

In [1]: format = '%Y-%d-%m %H:%M:%S%z'

In [2]: dates = pd.date_range('1900', '2000').tz_localize('+01:00').strftime(format).tolist()

In [3]: %%timeit
   ...: pd.to_datetime(dates, format=format)
   ...: 
   ...: 
234 ms ± 885 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)

@MarcoGorelli MarcoGorelli force-pushed the factor-out-some-directive-errors-from-strptime branch from b7b5193 to 7c57883 Compare December 17, 2022 09:36
Comment on lines -338 to -340
raise ValueError("Day of the year directive '%j' is not "
"compatible with ISO year directive '%G'. "
"Use '%Y' instead.")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one was never tested to begin with - have added a test to cover it

@MarcoGorelli MarcoGorelli added the Datetime Datetime data dtype label Dec 17, 2022
Comment on lines -632 to +633
"ISO week directive '%V' must be used with the ISO year directive "
"'%G' and a weekday directive '%A', '%a', '%w', or '%u'.",
"ISO week directive '%V' is incompatible with the year directive "
"'%Y'. Use the ISO year '%G' instead.",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've put the "%Y and %V used together" check before the "%V used without both %G and weekday directive", as I think it produces a more useful error message in this case

Comment on lines +739 to +744
[
"ISO week directive '%V' must be used with the ISO year directive "
"'%G' and a weekday directive '%A', '%a', '%w', or '%u'.",
"20 Monday",
"%V %A",
],
Copy link
Member Author

@MarcoGorelli MarcoGorelli Dec 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on upstream/main, this one would show "ISO week directive '%V' is incompatible with the year directive '%Y'", which is misleading because we didn't use "%Y"

raise ValueError("ISO year directive '%G' must be used with "
"the ISO week directive '%V' and a weekday "
"directive '%A', '%a', '%w', or '%u'.")
elif ("%V" in fmt and "%Y" in fmt):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
elif ("%V" in fmt and "%Y" in fmt):
elif "%V" in fmt and "%Y" in fmt:

Nit

@mroeschke mroeschke added this to the 2.0 milestone Dec 19, 2022
@mroeschke mroeschke merged commit d20c02b into pandas-dev:main Dec 19, 2022
@mroeschke
Copy link
Member

Thanks @MarcoGorelli

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: misleading message when using to_datetime with format='%V %a'
2 participants