Skip to content

Commit a327ad1

Browse files
authored
BUG: DataFrame with scalar tzaware Timestamp (#44518)
1 parent 6e6ebf4 commit a327ad1

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ Conversion
576576
- Bug in :class:`IntegerDtype` not allowing coercion from string dtype (:issue:`25472`)
577577
- Bug in :func:`to_datetime` with ``arg:xr.DataArray`` and ``unit="ns"`` specified raises TypeError (:issue:`44053`)
578578
- Bug in :meth:`DataFrame.convert_dtypes` not returning the correct type when a subclass does not overload :meth:`_constructor_sliced` (:issue:`43201`)
579+
- Bug in creating a :class:`DataFrame` from a timezone-aware :class:`Timestamp` scalar near a Daylight Savings Time transition (:issue:`42505`)
579580
-
580581

581582
Strings

pandas/core/arrays/datetimes.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -2084,8 +2084,13 @@ def _sequence_to_dt64ns(
20842084
)
20852085
if tz and inferred_tz:
20862086
# two timezones: convert to intended from base UTC repr
2087-
data = tzconversion.tz_convert_from_utc(data.view("i8"), tz)
2088-
data = data.view(DT64NS_DTYPE)
2087+
if data.dtype == "i8":
2088+
# GH#42505
2089+
# by convention, these are _already_ UTC, e.g
2090+
return data.view(DT64NS_DTYPE), tz, None
2091+
2092+
utc_vals = tzconversion.tz_convert_from_utc(data.view("i8"), tz)
2093+
data = utc_vals.view(DT64NS_DTYPE)
20892094
elif inferred_tz:
20902095
tz = inferred_tz
20912096

pandas/tests/frame/test_constructors.py

+13
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@
7070

7171

7272
class TestDataFrameConstructors:
73+
def test_constructor_dict_with_tzaware_scalar(self):
74+
# GH#42505
75+
dt = Timestamp("2019-11-03 01:00:00-0700").tz_convert("America/Los_Angeles")
76+
77+
df = DataFrame({"dt": dt}, index=[0])
78+
expected = DataFrame({"dt": [dt]})
79+
tm.assert_frame_equal(df, expected)
80+
81+
# Non-homogeneous
82+
df = DataFrame({"dt": dt, "value": [1]})
83+
expected = DataFrame({"dt": [dt], "value": [1]})
84+
tm.assert_frame_equal(df, expected)
85+
7386
def test_construct_ndarray_with_nas_and_int_dtype(self):
7487
# GH#26919 match Series by not casting np.nan to meaningless int
7588
arr = np.array([[1, np.nan], [2, 3]])

0 commit comments

Comments
 (0)