diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index f686a042c1a74..791365295c268 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -896,6 +896,7 @@ Timezones - 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`) - Bug when iterating over :class:`DatetimeIndex` that was localized with fixed timezone offset that rounded nanosecond precision to microseconds (:issue:`19603`) - Bug in :func:`DataFrame.diff` that raised an ``IndexError`` with tz-aware values (:issue:`18578`) +- Bug in :func:`melt` that converted tz-aware dtypes to tz-naive (:issue:`15785`) Offsets ^^^^^^^ diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py index 01445eb30a9e5..ce99d2f8c9a63 100644 --- a/pandas/core/reshape/melt.py +++ b/pandas/core/reshape/melt.py @@ -13,7 +13,9 @@ import re from pandas.core.dtypes.missing import notna +from pandas.core.dtypes.common import is_extension_type from pandas.core.tools.numeric import to_numeric +from pandas.core.reshape.concat import concat @Appender(_shared_docs['melt'] % @@ -70,7 +72,12 @@ def melt(frame, id_vars=None, value_vars=None, var_name=None, mdata = {} for col in id_vars: - mdata[col] = np.tile(frame.pop(col).values, K) + id_data = frame.pop(col) + if is_extension_type(id_data): + id_data = concat([id_data] * K, ignore_index=True) + else: + id_data = np.tile(id_data.values, K) + mdata[col] = id_data mcolumns = id_vars + var_name + [value_name] diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index 000b22d4fdd36..81570de7586de 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -212,6 +212,27 @@ def test_multiindex(self): res = self.df1.melt() assert res.columns.tolist() == ['CAP', 'low', 'value'] + @pytest.mark.parametrize("col", [ + pd.Series(pd.date_range('2010', periods=5, tz='US/Pacific')), + pd.Series(["a", "b", "c", "a", "d"], dtype="category"), + pd.Series([0, 1, 0, 0, 0])]) + def test_pandas_dtypes(self, col): + # GH 15785 + df = DataFrame({'klass': range(5), + 'col': col, + 'attr1': [1, 0, 0, 0, 0], + 'attr2': col}) + expected_value = pd.concat([pd.Series([1, 0, 0, 0, 0]), col], + ignore_index=True) + result = melt(df, id_vars=['klass', 'col'], var_name='attribute', + value_name='value') + expected = DataFrame({0: list(range(5)) * 2, + 1: pd.concat([col] * 2, ignore_index=True), + 2: ['attr1'] * 5 + ['attr2'] * 5, + 3: expected_value}) + expected.columns = ['klass', 'col', 'attribute', 'value'] + tm.assert_frame_equal(result, expected) + class TestLreshape(object):