Skip to content

Commit 271144a

Browse files
authored
BUG: Interval with Timestamp with tz shows tz (#55035)
* BUG: Interval with Timestamp with tz shows tz * Add whatsnew * Fix tests
1 parent e0c5b87 commit 271144a

File tree

7 files changed

+36
-41
lines changed

7 files changed

+36
-41
lines changed

doc/source/whatsnew/v2.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ Strings
257257

258258
Interval
259259
^^^^^^^^
260-
-
260+
- 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`)
261261
-
262262

263263
Indexing

pandas/_libs/interval.pyx

+3-18
Original file line numberDiff line numberDiff line change
@@ -478,31 +478,16 @@ cdef class Interval(IntervalMixin):
478478
args = (self.left, self.right, self.closed)
479479
return (type(self), args)
480480

481-
def _repr_base(self):
482-
left = self.left
483-
right = self.right
484-
485-
# TODO: need more general formatting methodology here
486-
if isinstance(left, _Timestamp) and isinstance(right, _Timestamp):
487-
left = left._short_repr
488-
right = right._short_repr
489-
490-
return left, right
491-
492481
def __repr__(self) -> str:
493-
494-
left, right = self._repr_base()
495-
disp = str if isinstance(left, np.generic) else repr
482+
disp = str if isinstance(self.left, (np.generic, _Timestamp)) else repr
496483
name = type(self).__name__
497-
repr_str = f"{name}({disp(left)}, {disp(right)}, closed={repr(self.closed)})"
484+
repr_str = f"{name}({disp(self.left)}, {disp(self.right)}, closed={repr(self.closed)})" # noqa: E501
498485
return repr_str
499486

500487
def __str__(self) -> str:
501-
502-
left, right = self._repr_base()
503488
start_symbol = "[" if self.closed_left else "("
504489
end_symbol = "]" if self.closed_right else ")"
505-
return f"{start_symbol}{left}, {right}{end_symbol}"
490+
return f"{start_symbol}{self.left}, {self.right}{end_symbol}"
506491

507492
def __add__(self, y):
508493
if (

pandas/_libs/tslibs/timestamps.pyx

-12
Original file line numberDiff line numberDiff line change
@@ -1078,18 +1078,6 @@ cdef class _Timestamp(ABCTimestamp):
10781078

10791079
return result
10801080

1081-
@property
1082-
def _short_repr(self) -> str:
1083-
# format a Timestamp with only _date_repr if possible
1084-
# otherwise _repr_base
1085-
if (self.hour == 0 and
1086-
self.minute == 0 and
1087-
self.second == 0 and
1088-
self.microsecond == 0 and
1089-
self.nanosecond == 0):
1090-
return self._date_repr
1091-
return self._repr_base
1092-
10931081
# -----------------------------------------------------------------
10941082
# Conversion Methods
10951083

pandas/core/indexes/interval.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1039,8 +1039,9 @@ def interval_range(
10391039
10401040
>>> pd.interval_range(start=pd.Timestamp('2017-01-01'),
10411041
... end=pd.Timestamp('2017-01-04'))
1042-
IntervalIndex([(2017-01-01, 2017-01-02], (2017-01-02, 2017-01-03],
1043-
(2017-01-03, 2017-01-04]],
1042+
IntervalIndex([(2017-01-01 00:00:00, 2017-01-02 00:00:00],
1043+
(2017-01-02 00:00:00, 2017-01-03 00:00:00],
1044+
(2017-01-03 00:00:00, 2017-01-04 00:00:00]],
10441045
dtype='interval[datetime64[ns], right]')
10451046
10461047
The ``freq`` parameter specifies the frequency between the left and right.
@@ -1056,8 +1057,9 @@ def interval_range(
10561057
10571058
>>> pd.interval_range(start=pd.Timestamp('2017-01-01'),
10581059
... periods=3, freq='MS')
1059-
IntervalIndex([(2017-01-01, 2017-02-01], (2017-02-01, 2017-03-01],
1060-
(2017-03-01, 2017-04-01]],
1060+
IntervalIndex([(2017-01-01 00:00:00, 2017-02-01 00:00:00],
1061+
(2017-02-01 00:00:00, 2017-03-01 00:00:00],
1062+
(2017-03-01 00:00:00, 2017-04-01 00:00:00]],
10611063
dtype='interval[datetime64[ns], right]')
10621064
10631065
Specify ``start``, ``end``, and ``periods``; the frequency is generated

pandas/tests/frame/methods/test_to_csv.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,6 @@ def test_to_csv_categorical_and_interval(self):
13261326
)
13271327
df["a"] = df["a"].astype("category")
13281328
result = df.to_csv()
1329-
expected_rows = [",a", '0,"[2020-01-01, 2020-01-02]"']
1329+
expected_rows = [",a", '0,"[2020-01-01 00:00:00, 2020-01-02 00:00:00]"']
13301330
expected = tm.convert_rows_list_to_csv_str(expected_rows)
13311331
assert result == expected

pandas/tests/indexes/interval/test_formats.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ def test_repr_floats(self):
8080
((Timestamp("20180102"), Timestamp("20180103"))),
8181
],
8282
"both",
83-
["[2018-01-01, 2018-01-02]", "NaN", "[2018-01-02, 2018-01-03]"],
83+
[
84+
"[2018-01-01 00:00:00, 2018-01-02 00:00:00]",
85+
"NaN",
86+
"[2018-01-02 00:00:00, 2018-01-03 00:00:00]",
87+
],
8488
),
8589
(
8690
[
@@ -103,3 +107,19 @@ def test_to_native_types(self, tuples, closed, expected_data):
103107
result = index._format_native_types()
104108
expected = np.array(expected_data)
105109
tm.assert_numpy_array_equal(result, expected)
110+
111+
def test_timestamp_with_timezone(self):
112+
# GH 55035
113+
index = IntervalIndex(
114+
[
115+
Interval(
116+
Timestamp("2020-01-01", tz="UTC"), Timestamp("2020-01-02", tz="UTC")
117+
)
118+
]
119+
)
120+
result = repr(index)
121+
expected = (
122+
"IntervalIndex([(2020-01-01 00:00:00+00:00, 2020-01-02 00:00:00+00:00]], "
123+
"dtype='interval[datetime64[ns, UTC], right]')"
124+
)
125+
assert result == expected

pandas/tests/io/excel/test_writers.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,10 @@ def test_multiindex_interval_datetimes(self, ext):
307307
[
308308
range(4),
309309
[
310-
"(2020-01-31, 2020-07-31]",
311-
"(2020-07-31, 2021-01-31]",
312-
"(2021-01-31, 2021-07-31]",
313-
"(2021-07-31, 2022-01-31]",
310+
"(2020-01-31 00:00:00, 2020-07-31 00:00:00]",
311+
"(2020-07-31 00:00:00, 2021-01-31 00:00:00]",
312+
"(2021-01-31 00:00:00, 2021-07-31 00:00:00]",
313+
"(2021-07-31 00:00:00, 2022-01-31 00:00:00]",
314314
],
315315
]
316316
),

0 commit comments

Comments
 (0)