Skip to content

Commit 6357d84

Browse files
committed
🐛 don't remove timezone-awareness when using the method from DataFrame
1 parent 2892b95 commit 6357d84

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ Datetimelike
701701
- Bug in :meth:`DatetimeIndex.strftime` and :meth:`Series.dt.strftime` where ``NaT`` was converted to the string ``'NaT'`` instead of ``np.nan`` (:issue:`29578`)
702702
- Bug in :attr:`Timestamp.resolution` being a property instead of a class attribute (:issue:`29910`)
703703
- Bug in :func:`pandas.to_datetime` when called with ``None`` raising ``TypeError`` instead of returning ``NaT`` (:issue:`30011`)
704+
- Bug in :meth:`DataFrame.append` would remove the timezone-awareness of new data (:issue:`30238`)
704705

705706
Timedelta
706707
^^^^^^^^^

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6765,7 +6765,7 @@ def append(self, other, ignore_index=False, verify_integrity=False, sort=None):
67656765
other.values.reshape((1, len(other))),
67666766
index=index,
67676767
columns=combined_columns,
6768-
)
6768+
).astype(other.dtype)
67696769
other = other._convert(datetime=True, timedelta=True)
67706770
if not self.columns.equals(combined_columns):
67716771
self = self.reindex(columns=combined_columns)

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_naize(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)