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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ Bug fixes
- Bug in :class:`AbstractHolidayCalendar` where timezone data was not propagated when computing holiday observances (:issue:`54580`)
- Bug in :class:`pandas.core.window.Rolling` where duplicate datetimelike indexes are treated as consecutive rather than equal with ``closed='left'`` and ``closed='neither'`` (:issue:`20712`)
- Bug in :meth:`DataFrame.apply` where passing ``raw=True`` ignored ``args`` passed to the applied function (:issue:`55009`)
- Bug in :meth:`pandas.DataFrame.melt` where it would not preserve the datetime (:issue:`55254`)
- Bug in :meth:`pandas.read_excel` with a ODS file without cached formatted cell for float values (:issue:`55219`)

Categorical
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/reshape/melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
mdata[value_name] = concat(
[frame.iloc[:, i] for i in range(frame.shape[1])]
).values
Expand Down
41 changes: 41 additions & 0 deletions pandas/tests/reshape/test_melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,47 @@ def test_melt_ea_columns(self):
)
tm.assert_frame_equal(result, expected)

def test_melt_preserves_datetime(self):
df = DataFrame(
data=[
{
"type": "A0",
"start_date": pd.Timestamp("2023/03/01", tz="Asia/Tokyo"),
"end_date": pd.Timestamp("2023/03/10", tz="Asia/Tokyo"),
},
{
"type": "A1",
"start_date": pd.Timestamp("2023/03/01", tz="Asia/Tokyo"),
"end_date": pd.Timestamp("2023/03/11", tz="Asia/Tokyo"),
},
],
index=["aaaa", "bbbb"],
)
result = df.melt(
id_vars=["type"],
value_vars=["start_date", "end_date"],
var_name="start/end",
value_name="date",
)
expected = DataFrame(
{
"type": {0: "A0", 1: "A1", 2: "A0", 3: "A1"},
"start/end": {
0: "start_date",
1: "start_date",
2: "end_date",
3: "end_date",
},
"date": {
0: pd.Timestamp("2023-03-01 00:00:00+0900", tz="Asia/Tokyo"),
1: pd.Timestamp("2023-03-01 00:00:00+0900", tz="Asia/Tokyo"),
2: pd.Timestamp("2023-03-10 00:00:00+0900", tz="Asia/Tokyo"),
3: pd.Timestamp("2023-03-11 00:00:00+0900", tz="Asia/Tokyo"),
},
}
)
tm.assert_frame_equal(result, expected)


class TestLreshape:
def test_pairs(self):
Expand Down