Skip to content

Commit fa4949f

Browse files
MarcoGorellijreback
authored andcommitted
[bug] don't remove timezone-awareness when using the method from Dat… (#30277)
1 parent 47ac4b3 commit fa4949f

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,7 @@ Datetimelike
714714
- Bug in :func:`pandas.to_datetime` failing for `deques` when using ``cache=True`` (the default) (:issue:`29403`)
715715
- 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`)
716716
- Bug in :class:`DatetimeIndex` addition when adding a non-optimized :class:`DateOffset` incorrectly dropping timezone information (:issue:`30336`)
717+
- Bug in :meth:`DataFrame.append` would remove the timezone-awareness of new data (:issue:`30238`)
717718

718719
Timedelta
719720
^^^^^^^^^

pandas/core/frame.py

+6-13
Original file line numberDiff line numberDiff line change
@@ -6757,25 +6757,18 @@ 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)
6766-
6760+
index = Index([other.name], name=self.index.name)
67676761
idx_diff = other.index.difference(self.columns)
67686762
try:
67696763
combined_columns = self.columns.append(idx_diff)
67706764
except TypeError:
67716765
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,
6766+
other = (
6767+
other.reindex(combined_columns, copy=False)
6768+
.to_frame()
6769+
.T.infer_objects()
6770+
.rename_axis(index.names, copy=False)
67776771
)
6778-
other = other._convert(datetime=True, timedelta=True)
67796772
if not self.columns.equals(combined_columns):
67806773
self = self.reindex(columns=combined_columns)
67816774
elif isinstance(other, list):

pandas/tests/frame/test_combine_concat.py

+11
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,17 @@ def test_append_dtypes(self):
288288
expected = DataFrame({"bar": Series([Timestamp("20130101"), 1])})
289289
tm.assert_frame_equal(result, expected)
290290

291+
@pytest.mark.parametrize(
292+
"timestamp", ["2019-07-19 07:04:57+0100", "2019-07-19 07:04:57"]
293+
)
294+
def test_append_timestamps_aware_or_naive(self, tz_naive_fixture, timestamp):
295+
# GH 30238
296+
tz = tz_naive_fixture
297+
df = pd.DataFrame([pd.Timestamp(timestamp, tz=tz)])
298+
result = df.append(df.iloc[0]).iloc[-1]
299+
expected = pd.Series(pd.Timestamp(timestamp, tz=tz), name=0)
300+
tm.assert_series_equal(result, expected)
301+
291302
def test_update(self):
292303
df = DataFrame(
293304
[[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)