149
149
>>> pd.arrays.IntervalArray([pd.Interval(0, 1), pd.Interval(1, 5)])
150
150
<IntervalArray>
151
151
[(0, 1], (1, 5]]
152
- Length: 2, closed: right, dtype: interval[int64]
152
+ Length: 2, closed: right, dtype: interval[int64, right ]
153
153
154
154
It may also be constructed using one of the constructor
155
155
methods: :meth:`IntervalArray.from_arrays`,
@@ -222,6 +222,9 @@ def _simple_new(
222
222
):
223
223
result = IntervalMixin .__new__ (cls )
224
224
225
+ if closed is None and isinstance (dtype , IntervalDtype ):
226
+ closed = dtype .closed
227
+
225
228
closed = closed or "right"
226
229
left = ensure_index (left , copy = copy )
227
230
right = ensure_index (right , copy = copy )
@@ -238,6 +241,12 @@ def _simple_new(
238
241
msg = f"dtype must be an IntervalDtype, got { dtype } "
239
242
raise TypeError (msg )
240
243
244
+ if dtype .closed is None :
245
+ # possibly loading an old pickle
246
+ dtype = IntervalDtype (dtype .subtype , closed )
247
+ elif closed != dtype .closed :
248
+ raise ValueError ("closed keyword does not match dtype.closed" )
249
+
241
250
# coerce dtypes to match if needed
242
251
if is_float_dtype (left ) and is_integer_dtype (right ):
243
252
right = right .astype (left .dtype )
@@ -279,9 +288,11 @@ def _simple_new(
279
288
# If these share data, then setitem could corrupt our IA
280
289
right = right .copy ()
281
290
291
+ dtype = IntervalDtype (left .dtype , closed = closed )
292
+ result ._dtype = dtype
293
+
282
294
result ._left = left
283
295
result ._right = right
284
- result ._closed = closed
285
296
if verify_integrity :
286
297
result ._validate ()
287
298
return result
@@ -343,7 +354,7 @@ def _from_factorized(cls, values, original):
343
354
>>> pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3])
344
355
<IntervalArray>
345
356
[(0, 1], (1, 2], (2, 3]]
346
- Length: 3, closed: right, dtype: interval[int64]
357
+ Length: 3, closed: right, dtype: interval[int64, right ]
347
358
"""
348
359
),
349
360
}
@@ -414,7 +425,7 @@ def from_breaks(
414
425
>>> pd.arrays.IntervalArray.from_arrays([0, 1, 2], [1, 2, 3])
415
426
<IntervalArray>
416
427
[(0, 1], (1, 2], (2, 3]]
417
- Length: 3, closed: right, dtype: interval[int64]
428
+ Length: 3, closed: right, dtype: interval[int64, right ]
418
429
"""
419
430
),
420
431
}
@@ -473,7 +484,7 @@ def from_arrays(
473
484
>>> pd.arrays.IntervalArray.from_tuples([(0, 1), (1, 2)])
474
485
<IntervalArray>
475
486
[(0, 1], (1, 2]]
476
- Length: 2, closed: right, dtype: interval[int64]
487
+ Length: 2, closed: right, dtype: interval[int64, right ]
477
488
"""
478
489
),
479
490
}
@@ -553,7 +564,7 @@ def _shallow_copy(self, left, right):
553
564
554
565
@property
555
566
def dtype (self ):
556
- return IntervalDtype ( self .left . dtype )
567
+ return self ._dtype
557
568
558
569
@property
559
570
def nbytes (self ) -> int :
@@ -1174,7 +1185,7 @@ def mid(self):
1174
1185
>>> intervals
1175
1186
<IntervalArray>
1176
1187
[(0, 1], (1, 3], (2, 4]]
1177
- Length: 3, closed: right, dtype: interval[int64]
1188
+ Length: 3, closed: right, dtype: interval[int64, right ]
1178
1189
"""
1179
1190
),
1180
1191
}
@@ -1203,7 +1214,7 @@ def closed(self):
1203
1214
Whether the intervals are closed on the left-side, right-side, both or
1204
1215
neither.
1205
1216
"""
1206
- return self ._closed
1217
+ return self .dtype . closed
1207
1218
1208
1219
_interval_shared_docs ["set_closed" ] = textwrap .dedent (
1209
1220
"""
@@ -1238,11 +1249,11 @@ def closed(self):
1238
1249
>>> index
1239
1250
<IntervalArray>
1240
1251
[(0, 1], (1, 2], (2, 3]]
1241
- Length: 3, closed: right, dtype: interval[int64]
1252
+ Length: 3, closed: right, dtype: interval[int64, right ]
1242
1253
>>> index.set_closed('both')
1243
1254
<IntervalArray>
1244
1255
[[0, 1], [1, 2], [2, 3]]
1245
- Length: 3, closed: both, dtype: interval[int64]
1256
+ Length: 3, closed: both, dtype: interval[int64, both ]
1246
1257
"""
1247
1258
),
1248
1259
}
@@ -1301,7 +1312,7 @@ def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray:
1301
1312
left = self ._left
1302
1313
right = self ._right
1303
1314
mask = self .isna ()
1304
- closed = self ._closed
1315
+ closed = self .closed
1305
1316
1306
1317
result = np .empty (len (left ), dtype = object )
1307
1318
for i in range (len (left )):
@@ -1441,7 +1452,7 @@ def repeat(self, repeats, axis=None):
1441
1452
>>> intervals
1442
1453
<IntervalArray>
1443
1454
[(0, 1], (1, 3], (2, 4]]
1444
- Length: 3, closed: right, dtype: interval[int64]
1455
+ Length: 3, closed: right, dtype: interval[int64, right ]
1445
1456
"""
1446
1457
),
1447
1458
}
0 commit comments