Skip to content

Commit

Permalink
Adjust methods for warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Roeschke committed Nov 15, 2018
1 parent 96da473 commit 9b5c8e7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
7 changes: 6 additions & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ def _to_m8(key, tz=None):
"""
if not isinstance(key, Timestamp):
# this also converts strings
key = Timestamp(key, tz=tz)
key = Timestamp(key)
if key.tz is not None and tz is not None:
# Don't tz_localize(None) if key was tz-aware already
key = key.tz_convert(tz)
else:
key = key.tz_localize(tz)

return np.int64(conversion.pydt_to_i8(key)).view(_NS_DTYPE)

Expand Down
6 changes: 3 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9337,9 +9337,9 @@ def describe_categorical_1d(data):
tz = data.dt.tz
asint = data.dropna().values.view('i8')
names += ['top', 'freq', 'first', 'last']
result += [Timestamp(top, tz=tz), freq,
Timestamp(asint.min(), tz=tz),
Timestamp(asint.max(), tz=tz)]
result += [Timestamp(top).tz_localize(tz), freq,
Timestamp(asint.min()).tz_localize(tz),
Timestamp(asint.max()).tz_localize(tz)]
else:
names += ['top', 'freq']
result += [top, freq]
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ def get_value(self, series, key):
def get_value_maybe_box(self, series, key):
# needed to localize naive datetimes
if self.tz is not None:
key = Timestamp(key, tz=self.tz)
key = Timestamp(key).tz_localize(self.tz)
elif not isinstance(key, Timestamp):
key = Timestamp(key)
values = self._engine.get_value(com.values_from_object(series),
Expand All @@ -966,9 +966,9 @@ def get_loc(self, key, method=None, tolerance=None):
tolerance = self._convert_tolerance(tolerance, np.asarray(key))

if isinstance(key, datetime):
# needed to localize naive datetimes
# localize tz-naive datetimes OR convert tz-aware datetimes
if key.tzinfo is None:
key = Timestamp(key, tz=self.tz)
key = Timestamp(key).tz_localize(self.tz)
else:
key = Timestamp(key).tz_convert(self.tz)
return Index.get_loc(self, key, method, tolerance)
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,7 @@ def _format_datetime64(x, tz=None, nat_rep='NaT'):
return nat_rep

if tz is not None or not isinstance(x, Timestamp):
x = Timestamp(x, tz=tz)
x = Timestamp(x).tz_localize(tz)

return str(x)

Expand Down

0 comments on commit 9b5c8e7

Please sign in to comment.