Skip to content
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

PERF: array_strptime #55898

Merged
merged 12 commits into from
Nov 26, 2023
Merged

Conversation

jbrockmendel
Copy link
Member

array_strptime is conceptually similar to array_to_datetime but stores local values instead of UTC values, then we pass the result through a localization pass that goes through object dtype. The upside of doing it the current way is that array_strptime does not need a fallback path for when mixed tzs/awareness is detected. The downside is everything else.

This changes array_strptime to store UTC values and follow the same pattern used by array_to_datetime (hopefully we can move toward sharing more code, though some of that will have to wait until deprecations are enforced in 3.0). It introduces a fallback _array_strptime_object_fallback that mirrors _array_to_datetime_object that we'll be able to get rid of once the mixed-tz deprecation is enforced in 3.0.

vals = np.array(["2016-01-01 02:03:04.567"] * 10**4, dtype=object)
vals2 = np.array([pd.Timestamp(x) for x in vals], dtype=object)
vals3 = np.array([x + "-01:00" for x in vals], dtype=object)

fmt = "%Y-%m-%d %H:%M:%S.%f"

%timeit pd.to_datetime(vals, format=fmt, cache=False)
2.39 ms ± 28.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)  # <- main
2.11 ms ± 45.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)  # <- PR

%timeit pd.to_datetime(vals2, format=fmt, cache=False)
23.9 ms ± 680 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)   # <- main
10.7 ms ± 113 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)  # <- PR


%timeit pd.to_datetime(vals3, format=fmt+"%z", cache=False)
99.7 ms ± 1.43 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)  # <- main
63.1 ms ± 1.65 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)  # <- PR

@mroeschke mroeschke added Datetime Datetime data dtype Performance Memory or execution speed performance labels Nov 13, 2023
@@ -723,6 +775,148 @@ cdef tzinfo _parse_with_format(
return tz


def _array_strptime_object_fallback(
ndarray[object] values,
Copy link
Member

Choose a reason for hiding this comment

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

Might need to be careful with performance of this during transition from Cython 0.29 to Cython 3. Definitely if this was just ndarray values you would lose performance if you didn't use PySequence_GetItem in the loop; I'm not sure if declaring it as ndarray[object] values makes a difference

Copy link
Member Author

Choose a reason for hiding this comment

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

should i update this to use PySequence_GetItem? im not too concerned with this function since it will be removed in 3.0 anyway

Copy link
Member

Choose a reason for hiding this comment

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

If it's going away soon probably not worth the effort

@mroeschke mroeschke added this to the 2.2 milestone Nov 23, 2023
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Changes look good

There's a lot of duplication between _array_strptime_object_fallback and array_strptime, but as far as I understand, _array_strptime_object_fallback can be removed in 3.0 anyway, right? If so, no bother

Does this need a whatsnew note for the perf improvement? (can be done as part of another PR, feel free to merge)

@jbrockmendel
Copy link
Member Author

but as far as I understand, _array_strptime_object_fallback can be removed in 3.0 anyway, right?

correct.

@mroeschke mroeschke merged commit 2421931 into pandas-dev:main Nov 26, 2023
40 checks passed
@mroeschke
Copy link
Member

Thanks @jbrockmendel

@jbrockmendel jbrockmendel deleted the perf-array_strptime branch November 26, 2023 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Performance Memory or execution speed performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants