Skip to content

Commit 4ffccaf

Browse files
authored
BUG: DataFrame with scalar tzaware Timestamp (#44518) (#44546)
1 parent 3b33fe4 commit 4ffccaf

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

pandas/core/arrays/datetimes.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -2067,8 +2067,13 @@ def sequence_to_dt64ns(
20672067
)
20682068
if tz and inferred_tz:
20692069
# two timezones: convert to intended from base UTC repr
2070-
data = tzconversion.tz_convert_from_utc(data.view("i8"), tz)
2071-
data = data.view(DT64NS_DTYPE)
2070+
if data.dtype == "i8":
2071+
# GH#42505
2072+
# by convention, these are _already_ UTC, e.g
2073+
return data.view(DT64NS_DTYPE), tz, None
2074+
2075+
utc_vals = tzconversion.tz_convert_from_utc(data.view("i8"), tz)
2076+
data = utc_vals.view(DT64NS_DTYPE)
20722077
elif inferred_tz:
20732078
tz = inferred_tz
20742079
elif allow_object and data.dtype == object:

pandas/tests/frame/test_constructors.py

+13
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@
6868

6969

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

0 commit comments

Comments
 (0)