Skip to content

Commit 7dd3ba5

Browse files
committed
Some cleanups
1 parent 4bf1862 commit 7dd3ba5

File tree

6 files changed

+32
-21
lines changed

6 files changed

+32
-21
lines changed

pandas/core/arrays/datetimelike.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,19 @@ def _scalar_type(self):
9999
raise AbstractMethodError(self)
100100

101101
def _scalar_from_string(self, value):
102-
# type: (str) -> Union[Period, Timestamp, Timedelta]
102+
# type: (str) -> Union[Period, Timestamp, Timedelta, NaT]
103+
"""
104+
Construct a scalar type from a string.
105+
106+
Parameters
107+
----------
108+
value : str
109+
110+
Returns
111+
-------
112+
Period, Timestamp, or Timedelt, or NaT
113+
Whatever the type of ``self._scalar_type`` is.
114+
"""
103115
raise AbstractMethodError(self)
104116

105117
def _unbox_scalar(self, value):
@@ -1096,7 +1108,7 @@ def _time_shift(self, periods, freq=None):
10961108
freq = frequencies.to_offset(freq)
10971109
offset = periods * freq
10981110
result = self + offset
1099-
if hasattr(self, 'tz'):
1111+
if getattr(self, 'tz', None):
11001112
result._dtype = DatetimeTZDtype(tz=self.tz)
11011113
return result
11021114

pandas/core/arrays/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ def __init__(self, values, dtype=_NS_DTYPE, freq=None, copy=False):
302302
if getattr(dtype, 'tz', None):
303303
# https://github.com/pandas-dev/pandas/issues/18595
304304
# Ensure that we have a standard timezone for pytz objects.
305-
# Without this, thins like adding an array of timedeltas and
305+
# Without this, things like adding an array of timedeltas and
306306
# a tz-aware Timestamp (with a tz specific to its datetime) will
307307
# be incorrect(ish?) for the array as a whole
308308
dtype = DatetimeTZDtype(tz=timezones.tz_standardize(dtype.tz))

pandas/core/arrays/period.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,10 @@ def _unbox_scalar(self, value):
240240
elif isinstance(value, self._scalar_type):
241241
return value.ordinal
242242
else:
243-
raise ValueError("'value' should be a Period")
243+
msg = (
244+
"'value' should be a Period. Got '{}' instead."
245+
)
246+
raise ValueError(msg.format(value))
244247

245248
def _scalar_from_string(self, value):
246249
# type: (str) -> Period

pandas/core/indexes/datetimelike.py

-3
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,6 @@ def _evaluate_compare(self, other, op):
146146

147147
def _ensure_localized(self, arg, ambiguous='raise', nonexistent='raise',
148148
from_utc=False):
149-
# This is a strange one. It seems like for for non-datetimetz
150-
# we just pass arg (an ndarray) through, while for datetimetz
151-
# we want to return a DatetimeIndex?
152149
result = self._values._ensure_localized(arg,
153150
ambiguous=ambiguous,
154151
nonexistent=nonexistent,

pandas/core/internals/blocks.py

+4-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import numpy as np
99

1010
from pandas._libs import internals as libinternals, lib, tslib, tslibs
11-
from pandas._libs.tslibs import Timedelta, conversion, timezones
11+
from pandas._libs.tslibs import Timedelta, conversion
1212
import pandas.compat as compat
1313
from pandas.compat import range, zip
1414
from pandas.errors import AbstractMethodError
@@ -3156,23 +3156,13 @@ def setitem(self, indexer, value):
31563156
# https://github.com/pandas-dev/pandas/issues/24020
31573157
# Need a dedicated setitem until #24020 (type promotion in setitem
31583158
# for extension arrays) is designed and implemented.
3159-
maybe_tz = getattr(value, 'tz', None)
3160-
return_object = (
3161-
(maybe_tz
3162-
and not timezones.tz_compare(self.values.tz, maybe_tz)) or
3163-
(lib.is_scalar(value)
3164-
and not isna(value)
3165-
and not value == tslib.iNaT
3166-
and not (isinstance(value, self.values._scalar_type) and
3167-
timezones.tz_compare(self.values.tz, maybe_tz)))
3168-
)
3169-
3170-
if return_object:
3159+
try:
3160+
return super(DatetimeTZBlock, self).setitem(indexer, value)
3161+
except ValueError:
31713162
newb = make_block(self.values.astype(object),
31723163
placement=self.mgr_locs,
31733164
klass=ObjectBlock,)
31743165
return newb.setitem(indexer, value)
3175-
return super(DatetimeTZBlock, self).setitem(indexer, value)
31763166

31773167

31783168
# -----------------------------------------------------------------

pandas/tests/series/test_datetime_values.py

+9
Original file line numberDiff line numberDiff line change
@@ -555,3 +555,12 @@ def test_setitem_with_string_index(self):
555555
x['Date'] = date.today()
556556
assert x.Date == date.today()
557557
assert x['Date'] == date.today()
558+
559+
def test_setitem_with_different_tz(self):
560+
ser = pd.Series(pd.date_range('2000', periods=2, tz="US/Central"))
561+
ser[0] = pd.Timestamp("2000", tz='US/Eastern')
562+
expected = pd.Series([
563+
pd.Timestamp("2000-01-01 00:00:00-05:00", tz="US/Eastern"),
564+
pd.Timestamp("2000-01-02 00:00:00-06:00", tz="US/Central"),
565+
], dtype=object)
566+
tm.assert_series_equal(ser, expected)

0 commit comments

Comments
 (0)