Skip to content

Commit 53bf291

Browse files
mroeschkejreback
authored andcommitted
BUG: Retain tz-aware dtypes with melt (#15785) (#20292)
1 parent c748de0 commit 53bf291

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,7 @@ Timezones
896896
- Bug in :func:`Timestamp.tz_localize` where localizing a timestamp near the minimum or maximum valid values could overflow and return a timestamp with an incorrect nanosecond value (:issue:`12677`)
897897
- Bug when iterating over :class:`DatetimeIndex` that was localized with fixed timezone offset that rounded nanosecond precision to microseconds (:issue:`19603`)
898898
- Bug in :func:`DataFrame.diff` that raised an ``IndexError`` with tz-aware values (:issue:`18578`)
899+
- Bug in :func:`melt` that converted tz-aware dtypes to tz-naive (:issue:`15785`)
899900

900901
Offsets
901902
^^^^^^^

pandas/core/reshape/melt.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
import re
1515
from pandas.core.dtypes.missing import notna
16+
from pandas.core.dtypes.common import is_extension_type
1617
from pandas.core.tools.numeric import to_numeric
18+
from pandas.core.reshape.concat import concat
1719

1820

1921
@Appender(_shared_docs['melt'] %
@@ -70,7 +72,12 @@ def melt(frame, id_vars=None, value_vars=None, var_name=None,
7072

7173
mdata = {}
7274
for col in id_vars:
73-
mdata[col] = np.tile(frame.pop(col).values, K)
75+
id_data = frame.pop(col)
76+
if is_extension_type(id_data):
77+
id_data = concat([id_data] * K, ignore_index=True)
78+
else:
79+
id_data = np.tile(id_data.values, K)
80+
mdata[col] = id_data
7481

7582
mcolumns = id_vars + var_name + [value_name]
7683

pandas/tests/reshape/test_melt.py

+21
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,27 @@ def test_multiindex(self):
212212
res = self.df1.melt()
213213
assert res.columns.tolist() == ['CAP', 'low', 'value']
214214

215+
@pytest.mark.parametrize("col", [
216+
pd.Series(pd.date_range('2010', periods=5, tz='US/Pacific')),
217+
pd.Series(["a", "b", "c", "a", "d"], dtype="category"),
218+
pd.Series([0, 1, 0, 0, 0])])
219+
def test_pandas_dtypes(self, col):
220+
# GH 15785
221+
df = DataFrame({'klass': range(5),
222+
'col': col,
223+
'attr1': [1, 0, 0, 0, 0],
224+
'attr2': col})
225+
expected_value = pd.concat([pd.Series([1, 0, 0, 0, 0]), col],
226+
ignore_index=True)
227+
result = melt(df, id_vars=['klass', 'col'], var_name='attribute',
228+
value_name='value')
229+
expected = DataFrame({0: list(range(5)) * 2,
230+
1: pd.concat([col] * 2, ignore_index=True),
231+
2: ['attr1'] * 5 + ['attr2'] * 5,
232+
3: expected_value})
233+
expected.columns = ['klass', 'col', 'attribute', 'value']
234+
tm.assert_frame_equal(result, expected)
235+
215236

216237
class TestLreshape(object):
217238

0 commit comments

Comments
 (0)