Skip to content

Commit cc45bf1

Browse files
committed
ENH: Improve from tuples
1 parent c9ee580 commit cc45bf1

File tree

2 files changed

+64
-37
lines changed

2 files changed

+64
-37
lines changed

pandas-stubs/core/indexes/interval.pyi

+8-9
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,20 @@ class IntervalIndex(IntervalMixin, ExtensionIndex, Generic[IntervalT]):
167167
) -> IntervalIndex[Interval[pd.Timedelta]]: ...
168168
@overload
169169
@classmethod
170-
def from_tuples(
170+
def from_tuples( # type:ignore[misc]
171171
cls,
172-
data: Sequence[tuple[int, int]] | npt.NDArray[np.integer],
172+
data: Sequence[tuple[int, int]],
173173
closed: IntervalClosedType = ...,
174174
name: Hashable = ...,
175175
copy: bool = ...,
176176
dtype: IntervalDtype | None = ...,
177177
) -> IntervalIndex[pd.Interval[int]]: ...
178+
# Ignore misc here due to intentional overlap between int and float
178179
@overload
179180
@classmethod
180181
def from_tuples(
181182
cls,
182-
data: Sequence[tuple[float, float]] | npt.NDArray[np.floating],
183+
data: Sequence[tuple[float, float]],
183184
closed: IntervalClosedType = ...,
184185
name: Hashable = ...,
185186
copy: bool = ...,
@@ -193,8 +194,7 @@ class IntervalIndex(IntervalMixin, ExtensionIndex, Generic[IntervalT]):
193194
tuple[pd.Timestamp, pd.Timestamp]
194195
| tuple[dt.datetime, dt.datetime]
195196
| tuple[np.datetime64, np.datetime64]
196-
]
197-
| npt.NDArray[np.datetime64],
197+
],
198198
closed: IntervalClosedType = ...,
199199
name: Hashable = ...,
200200
copy: bool = ...,
@@ -206,10 +206,9 @@ class IntervalIndex(IntervalMixin, ExtensionIndex, Generic[IntervalT]):
206206
cls,
207207
data: Sequence[
208208
tuple[pd.Timedelta, pd.Timedelta]
209-
| tuple[dt.datetime, dt.datetime]
210-
| tuple[np.datetime64, np.datetime64]
211-
]
212-
| npt.NDArray[np.timedelta64],
209+
| tuple[dt.timedelta, dt.timedelta]
210+
| tuple[np.timedelta64, np.timedelta64]
211+
],
213212
closed: IntervalClosedType = ...,
214213
name: Hashable = ...,
215214
copy: bool = ...,

tests/test_indexes.py

+56-28
Original file line numberDiff line numberDiff line change
@@ -456,24 +456,8 @@ def test_interval_index_arrays():
456456
),
457457
pd.IntervalIndex,
458458
)
459-
left_s_ts = pd.Series(
460-
[
461-
pd.Timestamp(2000, 1, 1),
462-
pd.Timestamp(2001, 1, 1),
463-
pd.Timestamp(2002, 1, 1),
464-
pd.Timestamp(2003, 1, 1),
465-
],
466-
dtype=pd.Timestamp,
467-
)
468-
right_s_ts = pd.Series(
469-
[
470-
pd.Timestamp(2001, 1, 1),
471-
pd.Timestamp(2002, 1, 1),
472-
pd.Timestamp(2003, 1, 1),
473-
pd.Timestamp(2004, 1, 1),
474-
],
475-
dtype=pd.Timestamp,
476-
)
459+
left_s_ts = pd.Series(pd.date_range("2000-01-01", "2003-01-01", freq="Y"))
460+
right_s_ts = pd.Series(pd.date_range("2001-01-01", "2004-01-01", freq="Y"))
477461
check(
478462
assert_type(
479463
pd.IntervalIndex.from_arrays(left_s_ts, right_s_ts),
@@ -504,9 +488,6 @@ def test_interval_index_arrays():
504488

505489

506490
def test_interval_index_tuples():
507-
int_arr: npt.NDArray[np.integer] = np.array([[1, 2], [3, 4]], dtype=np.int64)
508-
float_arr: npt.NDArray[np.floating] = np.array([[1, 2], [3, 4]], dtype=np.float_)
509-
510491
check(
511492
assert_type(
512493
pd.IntervalIndex.from_tuples([(1, 2), (2, 3)]),
@@ -516,33 +497,80 @@ def test_interval_index_tuples():
516497
)
517498
check(
518499
assert_type(
519-
pd.IntervalIndex.from_tuples(int_arr), "pd.IntervalIndex[pd.Interval[int]]"
500+
pd.IntervalIndex.from_tuples([(1.0, 2.0), (2.0, 3.0)]),
501+
"pd.IntervalIndex[pd.Interval[float]]",
520502
),
521503
pd.IntervalIndex,
522504
)
523505
check(
524506
assert_type(
525-
pd.IntervalIndex.from_tuples([(1.0, 2.0), (2.0, 3.0)]),
526-
"pd.IntervalIndex[pd.Interval[float]]",
507+
pd.IntervalIndex.from_tuples(
508+
[
509+
(pd.Timestamp(2000, 1, 1), pd.Timestamp(2001, 1, 1)),
510+
(pd.Timestamp(2001, 1, 1), pd.Timestamp(2002, 1, 1)),
511+
]
512+
),
513+
"pd.IntervalIndex[pd.Interval[pd.Timestamp]]",
527514
),
528515
pd.IntervalIndex,
529516
)
530517
check(
531518
assert_type(
532-
pd.IntervalIndex.from_tuples(float_arr),
533-
"pd.IntervalIndex[pd.Interval[float]]",
519+
pd.IntervalIndex.from_tuples(
520+
[
521+
(dt.datetime(2000, 1, 1), dt.datetime(2001, 1, 1)),
522+
(dt.datetime(2001, 1, 1), dt.datetime(2002, 1, 1)),
523+
]
524+
),
525+
"pd.IntervalIndex[pd.Interval[pd.Timestamp]]",
534526
),
535527
pd.IntervalIndex,
536528
)
537529
check(
538530
assert_type(
539531
pd.IntervalIndex.from_tuples(
540532
[
541-
(pd.Timestamp(2000, 1, 1), pd.Timestamp(2001, 1, 1)),
542-
(pd.Timestamp(2001, 1, 1), pd.Timestamp(2002, 1, 1)),
533+
(np.datetime64("2000-01-01"), np.datetime64("2001-01-01")),
534+
(np.datetime64("2001-01-01"), np.datetime64("2002-01-01")),
543535
]
544536
),
545537
"pd.IntervalIndex[pd.Interval[pd.Timestamp]]",
546538
),
547539
pd.IntervalIndex,
548540
)
541+
check(
542+
assert_type(
543+
pd.IntervalIndex.from_tuples(
544+
[
545+
(pd.Timedelta(1, "D"), pd.Timedelta(2, "D")),
546+
(pd.Timedelta(2, "D"), pd.Timedelta(3, "D")),
547+
]
548+
),
549+
"pd.IntervalIndex[pd.Interval[pd.Timedelta]]",
550+
),
551+
pd.IntervalIndex,
552+
)
553+
check(
554+
assert_type(
555+
pd.IntervalIndex.from_tuples(
556+
[
557+
(dt.timedelta(days=1), dt.timedelta(days=2)),
558+
(dt.timedelta(days=2), dt.timedelta(days=3)),
559+
]
560+
),
561+
"pd.IntervalIndex[pd.Interval[pd.Timedelta]]",
562+
),
563+
pd.IntervalIndex,
564+
)
565+
check(
566+
assert_type(
567+
pd.IntervalIndex.from_tuples(
568+
[
569+
(np.timedelta64(1, "D"), np.timedelta64(2, "D")),
570+
(np.timedelta64(2, "D"), np.timedelta64(3, "D")),
571+
]
572+
),
573+
"pd.IntervalIndex[pd.Interval[pd.Timedelta]]",
574+
),
575+
pd.IntervalIndex,
576+
)

0 commit comments

Comments
 (0)