Skip to content

ENH: Timestamp constructor now raises more explanatory error message #31653

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

fujiaxiang
Copy link
Member

Timestamp constructor now raises explanatory error message when year, month or day is missing

…(GH31200)

Timestamp constructor now raises explanatory error message when year, month or day is missing
@jreback jreback added Error Reporting Incorrect or improved errors from pandas Datetime Datetime data dtype labels Feb 5, 2020
@fujiaxiang
Copy link
Member Author

The error message of datetime.datetime() seems to depend on its version. On my computer I have TypeError: function missing required argument 'year' (pos 1), and in some of the CI environment as well as in @mroeschke 's comment in the original issue (#31200) the error is TypeError: Required argument 'month' (pos 2) not found. For compatibility I decided to use try-except block to extract the message from datetime.datetime() in the test.

The use of re.escape to match exact string in pytest is described in their official documentation.

@jbrockmendel
Copy link
Member

@fujiaxiang do you have a measure on how performance is affected? We don't call the constructor this way often, so a small hit is OK.

@fujiaxiang
Copy link
Member Author

@fujiaxiang do you have a measure on how performance is affected? We don't call the constructor this way often, so a small hit is OK.

@jbrockmendel I ran %timeit on current master and this implementation, this is what I get:

# on current master
>>>%timeit pd.Timestamp(year=2020, month=1, day=1)
1.19 µs ± 4.74 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

# re-built from this PR
>>>%timeit pd.Timestamp(year=2020, month=1, day=1)
1.7 µs ± 2.21 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Looks like it takes about 43% longer with this PR.
Let me know if you guys need a more accurate way of performance testing.

@fujiaxiang fujiaxiang requested a review from jreback February 10, 2020 13:25
@jreback jreback added this to the 1.1 milestone Feb 26, 2020
@jreback jreback merged commit 9bf3a28 into pandas-dev:master Feb 26, 2020
@jreback
Copy link
Contributor

jreback commented Feb 26, 2020

thanks

@fujiaxiang fujiaxiang deleted the timestamp_constructor_misleading_error_msg branch May 29, 2020 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

Successfully merging this pull request may close these issues.

raise more explanatory error when failing initialising pd.Timestamp
5 participants