Skip to content

Commit 7b4d39d

Browse files
MarcoGorelliMarco Gorelli
authored and
Marco Gorelli
committed
🐛 don't remove timezone-awareness when using the method from DataFrame
1 parent 3577b5a commit 7b4d39d

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ Datetimelike
713713
- Bug in :func:`pandas.to_datetime` failing for `deques` when using ``cache=True`` (the default) (:issue:`29403`)
714714
- Bug in :meth:`Series.item` with ``datetime64`` or ``timedelta64`` dtype, :meth:`DatetimeIndex.item`, and :meth:`TimedeltaIndex.item` returning an integer instead of a :class:`Timestamp` or :class:`Timedelta` (:issue:`30175`)
715715
- Bug in :class:`DatetimeIndex` addition when adding a non-optimized :class:`DateOffset` incorrectly dropping timezone information (:issue:`30336`)
716+
- Bug in :meth:`DataFrame.append` would remove the timezone-awareness of new data (:issue:`30238`)
716717

717718
Timedelta
718719
^^^^^^^^^

pandas/core/frame.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -6757,25 +6757,19 @@ def append(self, other, ignore_index=False, verify_integrity=False, sort=False):
67576757
" or if the Series has a name"
67586758
)
67596759

6760-
if other.name is None:
6761-
index = None
6762-
else:
6763-
# other must have the same index name as self, otherwise
6764-
# index name will be reset
6765-
index = Index([other.name], name=self.index.name)
6760+
index = Index([other.name], name=self.index.name)
67666761

67676762
idx_diff = other.index.difference(self.columns)
67686763
try:
67696764
combined_columns = self.columns.append(idx_diff)
67706765
except TypeError:
67716766
combined_columns = self.columns.astype(object).append(idx_diff)
6772-
other = other.reindex(combined_columns, copy=False)
6773-
other = DataFrame(
6774-
other.values.reshape((1, len(other))),
6775-
index=index,
6776-
columns=combined_columns,
6767+
other = (
6768+
other.reindex(combined_columns, copy=False)
6769+
.to_frame()
6770+
.T.rename_axis(index.names)
6771+
._convert(datetime=True, timedelta=True)
67776772
)
6778-
other = other._convert(datetime=True, timedelta=True)
67796773
if not self.columns.equals(combined_columns):
67806774
self = self.reindex(columns=combined_columns)
67816775
elif isinstance(other, list):

pandas/tests/frame/test_combine_concat.py

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import numpy as np
44
import pytest
5+
import pytz
56

67
import pandas as pd
78
from pandas import DataFrame, Index, Series, Timestamp, date_range
@@ -288,6 +289,20 @@ def test_append_dtypes(self):
288289
expected = DataFrame({"bar": Series([Timestamp("20130101"), 1])})
289290
tm.assert_frame_equal(result, expected)
290291

292+
@pytest.mark.parametrize(
293+
"timestamp, timezone",
294+
[
295+
("2019-07-19 07:04:57+0100", pytz.FixedOffset(60)),
296+
("2019-07-19 07:04:57", None),
297+
],
298+
)
299+
def test_append_timestamps_aware_or_naive(self, timestamp, timezone):
300+
# GH 30238
301+
df = pd.DataFrame([pd.Timestamp(timestamp, tz=timezone)])
302+
result = df.append(df.iloc[0]).iloc[-1]
303+
expected = pd.Series(pd.Timestamp(timestamp, tz=timezone), name=0)
304+
pd.testing.assert_series_equal(result, expected)
305+
291306
def test_update(self):
292307
df = DataFrame(
293308
[[1.5, np.nan, 3.0], [1.5, np.nan, 3.0], [1.5, np.nan, 3], [1.5, np.nan, 3]]

0 commit comments

Comments
 (0)