Skip to content

This fix enables you to preserve the datetime precision when using the melt method. #55270

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

Merged
merged 1 commit into from
Oct 3, 2023

Conversation

paulreece
Copy link
Contributor

@paulreece paulreece commented Sep 25, 2023

@@ -38,6 +38,7 @@ Bug fixes
- Fixed bug in :meth:`DataFrame.stack` with ``future_stack=True`` and columns a non-:class:`MultiIndex` consisting of tuples (:issue:`54948`)
- Fixed bug in :meth:`Series.dt.tz` with :class:`ArrowDtype` where a string was returned instead of a ``tzinfo`` object (:issue:`55003`)
- Fixed bug in :meth:`Series.pct_change` and :meth:`DataFrame.pct_change` showing unnecessary ``FutureWarning`` (:issue:`54981`)
- Fixed bug where using the melt method would not preserve the datetime (:issue:`55254`)
Copy link
Member

Choose a reason for hiding this comment

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

Can you move this to 2.2.0.rst?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@@ -134,7 +137,9 @@ def melt(

mcolumns = id_vars + var_name + [value_name]

if frame.shape[1] > 0:
if frame.shape[1] > 0 and not any(
isinstance(dt, DatetimeTZDtype) for dt in frame.dtypes.values
Copy link
Member

Choose a reason for hiding this comment

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

This may need to be more generic like

if not isinstance(id_data.dtype, np.dtype):

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ended up importing the types like so since pandas doesn't have a dtype method like numpy. Lmk if you had something else in mind.

pd_dtypes = (
        CategoricalDtype,
        DatetimeTZDtype,
        ExtensionDtype,
        IntervalDtype,
        PeriodDtype,
        SparseDtype,
    )

if frame.shape[1] > 0 and not any(
        isinstance(dt, pd_dtypes) for dt in frame.dtypes.values
    )

@mroeschke mroeschke added the Reshaping Concat, Merge/Join, Stack/Unstack, Explode label Sep 25, 2023
@paulreece paulreece force-pushed the melt_dt_fix branch 6 times, most recently from 06d8e02 to 7081895 Compare September 30, 2023 00:01
@@ -134,7 +134,9 @@ def melt(

mcolumns = id_vars + var_name + [value_name]

if frame.shape[1] > 0:
if frame.shape[1] > 0 and not any(
not isinstance(dt, np.dtype) and dt._supports_2d for dt in frame.dtypes.values
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
not isinstance(dt, np.dtype) and dt._supports_2d for dt in frame.dtypes.values
not isinstance(dt, np.dtype) and dt._supports_2d for dt in frame.dtypes

@mroeschke mroeschke added this to the 2.2 milestone Oct 3, 2023
@mroeschke mroeschke merged commit 2245217 into pandas-dev:main Oct 3, 2023
@mroeschke
Copy link
Member

Nice work @paulreece

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: melt method doesn't seem to preserve timezone settings.
2 participants