-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Conversation
2344d65
to
975ed29
Compare
doc/source/whatsnew/v2.1.1.rst
Outdated
@@ -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`) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
pandas/core/reshape/melt.py
Outdated
@@ -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 |
There was a problem hiding this comment.
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
pandas/pandas/core/reshape/melt.py
Line 125 in e86ed37
if not isinstance(id_data.dtype, np.dtype): |
There was a problem hiding this comment.
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
)
975ed29
to
e460135
Compare
06d8e02
to
7081895
Compare
pandas/core/reshape/melt.py
Outdated
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
…e melt method. It fixes the issues raised in pandas-dev#55254.
Nice work @paulreece |
doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.