Skip to content

Commit 4638603

Browse files
mroeschketm9k1
authored andcommitted
BUG: Append DataFrame to Series with dateutil timezone (pandas-dev#23685)
1 parent 22a42e7 commit 4638603

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,7 @@ Reshaping
14151415
- Bug in :func:`pandas.concat` when concatenating a multicolumn DataFrame with tz-aware data against a DataFrame with a different number of columns (:issue:`22796`)
14161416
- Bug in :func:`merge_asof` where confusing error message raised when attempting to merge with missing values (:issue:`23189`)
14171417
- Bug in :meth:`DataFrame.nsmallest` and :meth:`DataFrame.nlargest` for dataframes that have a :class:`MultiIndex` for columns (:issue:`23033`).
1418+
- Bug in :meth:`DataFrame.append` with a :class:`Series` with a dateutil timezone would raise a ``TypeError`` (:issue:`23682`)
14181419

14191420
.. _whatsnew_0240.bug_fixes.sparse:
14201421

pandas/_libs/lib.pyx

+12-11
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ cdef extern from "src/parse_helper.h":
5050
int floatify(object, float64_t *result, int *maybe_int) except -1
5151

5252
cimport util
53-
from util cimport (is_nan,
54-
UINT8_MAX, UINT64_MAX, INT64_MAX, INT64_MIN)
53+
from util cimport is_nan, UINT64_MAX, INT64_MAX, INT64_MIN
5554

5655
from tslib import array_to_datetime
5756
from tslibs.nattype cimport NPY_NAT
@@ -1677,20 +1676,22 @@ def is_datetime_with_singletz_array(values: ndarray) -> bool:
16771676

16781677
if n == 0:
16791678
return False
1680-
1679+
# Get a reference timezone to compare with the rest of the tzs in the array
16811680
for i in range(n):
16821681
base_val = values[i]
16831682
if base_val is not NaT:
16841683
base_tz = get_timezone(getattr(base_val, 'tzinfo', None))
1685-
1686-
for j in range(i, n):
1687-
val = values[j]
1688-
if val is not NaT:
1689-
tz = getattr(val, 'tzinfo', None)
1690-
if not tz_compare(base_tz, tz):
1691-
return False
16921684
break
16931685

1686+
for j in range(i, n):
1687+
# Compare val's timezone with the reference timezone
1688+
# NaT can coexist with tz-aware datetimes, so skip if encountered
1689+
val = values[j]
1690+
if val is not NaT:
1691+
tz = getattr(val, 'tzinfo', None)
1692+
if not tz_compare(base_tz, tz):
1693+
return False
1694+
16941695
return True
16951696

16961697

@@ -2080,7 +2081,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
20802081

20812082
# we try to coerce datetime w/tz but must all have the same tz
20822083
if seen.datetimetz_:
2083-
if len({getattr(val, 'tzinfo', None) for val in objects}) == 1:
2084+
if is_datetime_with_singletz_array(objects):
20842085
from pandas import DatetimeIndex
20852086
return DatetimeIndex(objects)
20862087
seen.object_ = 1

pandas/tests/reshape/test_concat.py

+15
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,21 @@ def test_append_missing_column_proper_upcast(self, sort):
10101010
assert appended['A'].dtype == 'f8'
10111011
assert appended['B'].dtype == 'O'
10121012

1013+
def test_append_empty_frame_to_series_with_dateutil_tz(self):
1014+
# GH 23682
1015+
date = Timestamp('2018-10-24 07:30:00', tz=dateutil.tz.tzutc())
1016+
s = Series({'date': date, 'a': 1.0, 'b': 2.0})
1017+
df = DataFrame(columns=['c', 'd'])
1018+
result = df.append(s, ignore_index=True)
1019+
expected = DataFrame([[np.nan, np.nan, 1., 2., date]],
1020+
columns=['c', 'd', 'a', 'b', 'date'])
1021+
# These columns get cast to object after append
1022+
object_cols = ['c', 'd', 'date']
1023+
expected.loc[:, object_cols] = expected.loc[:, object_cols].astype(
1024+
object
1025+
)
1026+
assert_frame_equal(result, expected)
1027+
10131028

10141029
class TestConcatenate(ConcatenateBase):
10151030

0 commit comments

Comments
 (0)