Skip to content

BUG: Retain tz-aware dtypes with melt (#15785) #20292

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 4 commits into from
Mar 13, 2018
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/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^
Expand Down
9 changes: 8 additions & 1 deletion pandas/core/reshape/melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'] %
Expand Down Expand Up @@ -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]

Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/reshape/test_melt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down