Skip to content

Commit

Permalink
This fix enables you to preserve the datetime precision when using th…
Browse files Browse the repository at this point in the history
…e melt method. It fixes the issues raised in pandas-dev#55254.
  • Loading branch information
paulreece committed Sep 30, 2023
1 parent 61d2056 commit 7081895
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
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 @@ -250,6 +250,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(
hasattr(dt, "_supports_2d") and dt._supports_2d for dt in frame.dtypes.values
):
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

0 comments on commit 7081895

Please sign in to comment.