Skip to content

Commit a348f91

Browse files
MarcoGorelliMarco Gorelli
authored and
Marco Gorelli
committed
🐛 don't remove timezone-awareness when using the method from DataFrame
1 parent 37dfcc1 commit a348f91

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
@@ -702,6 +702,7 @@ Datetimelike
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`)
704704
- Bug in :func:`pandas.to_datetime` failing for `deques` when using ``cache=True`` (the default) (:issue:`29403`)
705+
- Bug in :meth:`DataFrame.append` would remove the timezone-awareness of new data (:issue:`30238`)
705706

706707
Timedelta
707708
^^^^^^^^^

pandas/core/frame.py

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

6749-
if other.name is None:
6750-
index = None
6751-
else:
6752-
# other must have the same index name as self, otherwise
6753-
# index name will be reset
6754-
index = Index([other.name], name=self.index.name)
6749+
index = Index([other.name], name=self.index.name)
67556750

67566751
idx_diff = other.index.difference(self.columns)
67576752
try:
67586753
combined_columns = self.columns.append(idx_diff)
67596754
except TypeError:
67606755
combined_columns = self.columns.astype(object).append(idx_diff)
6761-
other = other.reindex(combined_columns, copy=False)
6762-
other = DataFrame(
6763-
other.values.reshape((1, len(other))),
6764-
index=index,
6765-
columns=combined_columns,
6756+
other = (
6757+
other.reindex(combined_columns, copy=False)
6758+
.to_frame()
6759+
.T.rename_axis(index.names)
6760+
._convert(datetime=True, timedelta=True)
67666761
)
6767-
other = other._convert(datetime=True, timedelta=True)
67686762
if not self.columns.equals(combined_columns):
67696763
self = self.reindex(columns=combined_columns)
67706764
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)