Skip to content

Commit

Permalink
BUG: Interval with Timestamp with tz shows tz (pandas-dev#55035)
Browse files Browse the repository at this point in the history
* BUG: Interval with Timestamp with tz shows tz

* Add whatsnew

* Fix tests
  • Loading branch information
mroeschke authored and hedeershowk committed Sep 20, 2023
1 parent b4fbc7a commit 10e948e
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 41 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ Strings

Interval
^^^^^^^^
-
- Bug in :class:`Interval` ``__repr__`` not displaying UTC offsets for :class:`Timestamp` bounds. Additionally the hour, minute and second components will now be shown. (:issue:`55015`)
-

Indexing
Expand Down
21 changes: 3 additions & 18 deletions pandas/_libs/interval.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -478,31 +478,16 @@ cdef class Interval(IntervalMixin):
args = (self.left, self.right, self.closed)
return (type(self), args)

def _repr_base(self):
left = self.left
right = self.right

# TODO: need more general formatting methodology here
if isinstance(left, _Timestamp) and isinstance(right, _Timestamp):
left = left._short_repr
right = right._short_repr

return left, right

def __repr__(self) -> str:

left, right = self._repr_base()
disp = str if isinstance(left, np.generic) else repr
disp = str if isinstance(self.left, (np.generic, _Timestamp)) else repr
name = type(self).__name__
repr_str = f"{name}({disp(left)}, {disp(right)}, closed={repr(self.closed)})"
repr_str = f"{name}({disp(self.left)}, {disp(self.right)}, closed={repr(self.closed)})" # noqa: E501
return repr_str

def __str__(self) -> str:

left, right = self._repr_base()
start_symbol = "[" if self.closed_left else "("
end_symbol = "]" if self.closed_right else ")"
return f"{start_symbol}{left}, {right}{end_symbol}"
return f"{start_symbol}{self.left}, {self.right}{end_symbol}"

def __add__(self, y):
if (
Expand Down
12 changes: 0 additions & 12 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1078,18 +1078,6 @@ cdef class _Timestamp(ABCTimestamp):

return result

@property
def _short_repr(self) -> str:
# format a Timestamp with only _date_repr if possible
# otherwise _repr_base
if (self.hour == 0 and
self.minute == 0 and
self.second == 0 and
self.microsecond == 0 and
self.nanosecond == 0):
return self._date_repr
return self._repr_base

# -----------------------------------------------------------------
# Conversion Methods

Expand Down
10 changes: 6 additions & 4 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,8 +1039,9 @@ def interval_range(
>>> pd.interval_range(start=pd.Timestamp('2017-01-01'),
... end=pd.Timestamp('2017-01-04'))
IntervalIndex([(2017-01-01, 2017-01-02], (2017-01-02, 2017-01-03],
(2017-01-03, 2017-01-04]],
IntervalIndex([(2017-01-01 00:00:00, 2017-01-02 00:00:00],
(2017-01-02 00:00:00, 2017-01-03 00:00:00],
(2017-01-03 00:00:00, 2017-01-04 00:00:00]],
dtype='interval[datetime64[ns], right]')
The ``freq`` parameter specifies the frequency between the left and right.
Expand All @@ -1056,8 +1057,9 @@ def interval_range(
>>> pd.interval_range(start=pd.Timestamp('2017-01-01'),
... periods=3, freq='MS')
IntervalIndex([(2017-01-01, 2017-02-01], (2017-02-01, 2017-03-01],
(2017-03-01, 2017-04-01]],
IntervalIndex([(2017-01-01 00:00:00, 2017-02-01 00:00:00],
(2017-02-01 00:00:00, 2017-03-01 00:00:00],
(2017-03-01 00:00:00, 2017-04-01 00:00:00]],
dtype='interval[datetime64[ns], right]')
Specify ``start``, ``end``, and ``periods``; the frequency is generated
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/methods/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,6 @@ def test_to_csv_categorical_and_interval(self):
)
df["a"] = df["a"].astype("category")
result = df.to_csv()
expected_rows = [",a", '0,"[2020-01-01, 2020-01-02]"']
expected_rows = [",a", '0,"[2020-01-01 00:00:00, 2020-01-02 00:00:00]"']
expected = tm.convert_rows_list_to_csv_str(expected_rows)
assert result == expected
22 changes: 21 additions & 1 deletion pandas/tests/indexes/interval/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ def test_repr_floats(self):
((Timestamp("20180102"), Timestamp("20180103"))),
],
"both",
["[2018-01-01, 2018-01-02]", "NaN", "[2018-01-02, 2018-01-03]"],
[
"[2018-01-01 00:00:00, 2018-01-02 00:00:00]",
"NaN",
"[2018-01-02 00:00:00, 2018-01-03 00:00:00]",
],
),
(
[
Expand All @@ -103,3 +107,19 @@ def test_to_native_types(self, tuples, closed, expected_data):
result = index._format_native_types()
expected = np.array(expected_data)
tm.assert_numpy_array_equal(result, expected)

def test_timestamp_with_timezone(self):
# GH 55035
index = IntervalIndex(
[
Interval(
Timestamp("2020-01-01", tz="UTC"), Timestamp("2020-01-02", tz="UTC")
)
]
)
result = repr(index)
expected = (
"IntervalIndex([(2020-01-01 00:00:00+00:00, 2020-01-02 00:00:00+00:00]], "
"dtype='interval[datetime64[ns, UTC], right]')"
)
assert result == expected
8 changes: 4 additions & 4 deletions pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,10 @@ def test_multiindex_interval_datetimes(self, ext):
[
range(4),
[
"(2020-01-31, 2020-07-31]",
"(2020-07-31, 2021-01-31]",
"(2021-01-31, 2021-07-31]",
"(2021-07-31, 2022-01-31]",
"(2020-01-31 00:00:00, 2020-07-31 00:00:00]",
"(2020-07-31 00:00:00, 2021-01-31 00:00:00]",
"(2021-01-31 00:00:00, 2021-07-31 00:00:00]",
"(2021-07-31 00:00:00, 2022-01-31 00:00:00]",
],
]
),
Expand Down

0 comments on commit 10e948e

Please sign in to comment.