Skip to content

ENH: consistency of input args for boundaries - Interval #46522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 89 commits into from
May 30, 2022
Merged
Changes from 1 commit
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
538ba43
Update interval.pyi
Mar 26, 2022
a9da7bd
Update interval.pyx
Mar 26, 2022
b222280
Update intervaltree.pxi.in
Mar 26, 2022
466f35a
Update lib.pyx
Mar 26, 2022
323f0d8
Update asserters.py
Mar 26, 2022
c4931c9
Update _arrow_utils.py
Mar 26, 2022
40bfa76
Update interval.py
Mar 26, 2022
23ad9c8
Update cast.py
Mar 26, 2022
f700477
Update dtypes.py
Mar 26, 2022
3835d29
Update interval.py
Mar 26, 2022
6ac2f6a
Update blocks.py
Mar 26, 2022
e53605d
Update tile.py
Mar 26, 2022
8a8f38c
first
Mar 26, 2022
97f7b9e
add
Mar 27, 2022
d20b1be
Merge branch 'main' into enh-consistency-interval-index
Mar 27, 2022
a8a3266
pre commit
Mar 27, 2022
a4d58dc
pre commit
Mar 27, 2022
edf53b7
pre commit
Mar 27, 2022
d0f17c0
pre commit
Mar 27, 2022
b00a947
pre commit
Mar 27, 2022
8f3991d
interval
Mar 27, 2022
5bc735b
doc
Mar 27, 2022
eb3d9ec
doc
Mar 27, 2022
0a41668
doc
Mar 27, 2022
c1f9de4
pre commit
Mar 27, 2022
7d9db8b
pre commit
Mar 27, 2022
49b35cf
pre commit
Mar 27, 2022
4394ad4
Merge branch 'main' of https://github.com/weikhor/pandas into enh-con…
Mar 28, 2022
5703c01
test
Mar 28, 2022
8f6fa54
Merge branch 'main' into enh-consistency-interval-index
Mar 28, 2022
816da28
test
Mar 29, 2022
b1dead8
Merge branch 'main' into enh-consistency-interval-index
Mar 29, 2022
3efc78f
test
Mar 30, 2022
2a9d176
Merge branch 'main' into enh-consistency-interval-index
Mar 30, 2022
59005ec
test
Mar 30, 2022
be88126
Update v0.20.0.rst
Mar 30, 2022
5588b1a
Update interval.py
Mar 30, 2022
217348b
Update interval.py
Mar 30, 2022
7260fcb
Update tile.py
Mar 30, 2022
ddeac18
Merge branch 'main' into enh-consistency-interval-index
Mar 30, 2022
707fd4a
test
Mar 31, 2022
340b66d
Merge branch 'main' into enh-consistency-interval-index
Mar 31, 2022
12e93d5
add
Mar 31, 2022
90eea5c
test
Mar 31, 2022
865e9e4
doc
Mar 31, 2022
59f4681
doc
Apr 2, 2022
e0668d3
doc
Apr 2, 2022
9acc1ba
Merge branch 'main' into enh-consistency-interval-index
Apr 2, 2022
f18fe3a
test
Apr 2, 2022
72d5a0c
test
Apr 3, 2022
3e8fc9a
:Merge branch 'main' of https://github.com/weikhor/pandas into enh-co…
Apr 3, 2022
9461b42
test
Apr 3, 2022
14e9311
test
Apr 3, 2022
eb39213
test
Apr 3, 2022
eb2e95a
test
Apr 3, 2022
4713022
test
Apr 3, 2022
fafd961
test
Apr 3, 2022
611a27f
test
Apr 3, 2022
e339d4d
Merge branch 'main' into enh-consistency-interval-index
Apr 4, 2022
bd2a573
Merge branch 'main' into enh-consistency-interval-index
Apr 5, 2022
1bb0474
Merge branch 'main' of https://github.com/weikhor/pandas into enh-con…
Apr 9, 2022
56d7b25
pre commit
Apr 9, 2022
90ceec4
Merge branch 'main' into enh-consistency-interval-index
Apr 14, 2022
27ea3a8
Merge branch 'main' into enh-consistency-interval-index
Apr 17, 2022
74f3a0f
Merge branch 'main' into enh-consistency-interval-index
Apr 18, 2022
45b318f
Merge branch 'main' into enh-consistency-interval-index
Apr 28, 2022
86a67b0
Merge branch 'main' into enh-consistency-interval-index
Apr 28, 2022
d4c2212
add
Apr 29, 2022
c0653ee
add helper function
May 16, 2022
4f6b499
Merge branch 'main' into enh-consistency-interval-index
May 16, 2022
67922fc
Merge branch 'main' into enh-consistency-interval-index
May 16, 2022
c32384e
interval
May 16, 2022
6b8cd69
Merge branch 'main' into enh-consistency-interval-index
May 18, 2022
eab8b58
update warning_interval
May 18, 2022
f234f07
resolve conflict
May 19, 2022
ff53d3f
Merge branch 'main' into enh-consistency-interval-index
May 19, 2022
fe62e50
pickle
May 19, 2022
a73ec91
Update dtypes.py
May 19, 2022
12c7806
Merge branch 'main' into enh-consistency-interval-index
May 20, 2022
757cabf
pickle
May 24, 2022
82d58b0
pickle
May 24, 2022
4e3d167
mrege conflict
May 26, 2022
03890df
resolve warning
May 26, 2022
c8c6193
Merge branch 'main' into enh-consistency-interval-index
May 27, 2022
41f59fb
mypy
May 28, 2022
17b54ad
Merge branch 'main' into enh-consistency-interval-index
May 28, 2022
b73f4ba
Merge branch 'main' into enh-consistency-interval-index
May 28, 2022
8cbeca0
Merge branch 'main' into enh-consistency-interval-index
May 29, 2022
35b18ea
Merge branch 'main' into enh-consistency-interval-index
May 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update dtypes.py
Khor Chean Wei authored Mar 26, 2022
commit f700477c19a527e6348604bad721ce8a567c0850
63 changes: 35 additions & 28 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
@@ -1038,7 +1038,7 @@ class IntervalDtype(PandasExtensionDtype):
Examples
--------
>>> pd.IntervalDtype(subtype='int64', closed='both')
>>> pd.IntervalDtype(subtype='int64', inclusive='both')
interval[int64, both]
"""

@@ -1049,35 +1049,42 @@ class IntervalDtype(PandasExtensionDtype):
num = 103
_metadata = (
"subtype",
"closed",
"inclusive",
)
_match = re.compile(
r"(I|i)nterval\[(?P<subtype>[^,]+)(, (?P<closed>(right|left|both|neither)))?\]"
r"(I|i)nterval\[(?P<subtype>[^,]+)(, (?P<inclusive>(right|left|both|neither)))?\]"
)
_cache_dtypes: dict[str_type, PandasExtensionDtype] = {}

def __new__(cls, subtype=None, closed: str_type | None = None):
def __new__(cls, subtype=None, inclusive: str_type | None = None):
from pandas.core.dtypes.common import (
is_string_dtype,
pandas_dtype,
)

if closed is not None and closed not in {"right", "left", "both", "neither"}:
raise ValueError("closed must be one of 'right', 'left', 'both', 'neither'")
if inclusive is not None and inclusive not in {
"right",
"left",
"both",
"neither",
}:
raise ValueError(
"inclusive must be one of 'right', 'left', 'both', 'neither'"
)

if isinstance(subtype, IntervalDtype):
if closed is not None and closed != subtype.closed:
if inclusive is not None and inclusive != subtype.inclusive:
raise ValueError(
"dtype.closed and 'closed' do not match. "
"Try IntervalDtype(dtype.subtype, closed) instead."
"dtype.inclusive and 'inclusive' do not match. "
"Try IntervalDtype(dtype.subtype, inclusive) instead."
)
return subtype
elif subtype is None:
# we are called as an empty constructor
# generally for pickle compat
u = object.__new__(cls)
u._subtype = None
u._closed = closed
u._closed = inclusive
return u
elif isinstance(subtype, str) and subtype.lower() == "interval":
subtype = None
@@ -1087,14 +1094,14 @@ def __new__(cls, subtype=None, closed: str_type | None = None):
if m is not None:
gd = m.groupdict()
subtype = gd["subtype"]
if gd.get("closed", None) is not None:
if closed is not None:
if closed != gd["closed"]:
if gd.get("inclusive", None) is not None:
if inclusive is not None:
if inclusive != gd["inclusive"]:
raise ValueError(
"'closed' keyword does not match value "
"'inclusive' keyword does not match value "
"specified in dtype string"
)
closed = gd["closed"]
inclusive = gd["inclusive"]

try:
subtype = pandas_dtype(subtype)
@@ -1109,13 +1116,13 @@ def __new__(cls, subtype=None, closed: str_type | None = None):
)
raise TypeError(msg)

key = str(subtype) + str(closed)
key = str(subtype) + str(inclusive)
try:
return cls._cache_dtypes[key]
except KeyError:
u = object.__new__(cls)
u._subtype = subtype
u._closed = closed
u._closed = inclusive
cls._cache_dtypes[key] = u
return u

@@ -1132,7 +1139,7 @@ def _can_hold_na(self) -> bool:
return True

@property
def closed(self):
def inclusive(self):
return self._closed

@property
@@ -1184,10 +1191,10 @@ def type(self):
def __str__(self) -> str_type:
if self.subtype is None:
return "interval"
if self.closed is None:
if self.inclusive is None:
# Only partially initialized GH#38394
return f"interval[{self.subtype}]"
return f"interval[{self.subtype}, {self.closed}]"
return f"interval[{self.subtype}, {self.inclusive}]"

def __hash__(self) -> int:
# make myself hashable
@@ -1201,7 +1208,7 @@ def __eq__(self, other: Any) -> bool:
elif self.subtype is None or other.subtype is None:
# None should match any subtype
return True
elif self.closed != other.closed:
elif self.inclusive != other.inclusive:
return False
else:
from pandas.core.dtypes.common import is_dtype_equal
@@ -1214,8 +1221,8 @@ def __setstate__(self, state):
# pickle -> need to set the settable private ones here (see GH26067)
self._subtype = state["subtype"]

# backward-compat older pickles won't have "closed" key
self._closed = state.pop("closed", None)
# backward-compat older pickles won't have "inclusive" key
self._closed = state.pop("inclusive", None)

@classmethod
def is_dtype(cls, dtype: object) -> bool:
@@ -1257,31 +1264,31 @@ def __from_arrow__(
arr = arr.storage
left = np.asarray(arr.field("left"), dtype=self.subtype)
right = np.asarray(arr.field("right"), dtype=self.subtype)
iarr = IntervalArray.from_arrays(left, right, closed=self.closed)
iarr = IntervalArray.from_arrays(left, right, inclusive=self.inclusive)
results.append(iarr)

if not results:
return IntervalArray.from_arrays(
np.array([], dtype=self.subtype),
np.array([], dtype=self.subtype),
closed=self.closed,
inclusive=self.inclusive,
)
return IntervalArray._concat_same_type(results)

def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None:
if not all(isinstance(x, IntervalDtype) for x in dtypes):
return None

closed = cast("IntervalDtype", dtypes[0]).closed
if not all(cast("IntervalDtype", x).closed == closed for x in dtypes):
inclusive = cast("IntervalDtype", dtypes[0]).inclusive
if not all(cast("IntervalDtype", x).inclusive == inclusive for x in dtypes):
return np.dtype(object)

from pandas.core.dtypes.cast import find_common_type

common = find_common_type([cast("IntervalDtype", x).subtype for x in dtypes])
if common == object:
return np.dtype(object)
return IntervalDtype(common, closed=closed)
return IntervalDtype(common, inclusive=inclusive)


class PandasDtype(ExtensionDtype):