-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
CLN: various related to numeric indexes #41615
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
jreback
merged 7 commits into
pandas-dev:master
from
topper-123:clean_various_numeric_index
Jun 3, 2021
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
474d327
CLN: various related to numeric indexes
topper-123 ec0e5e2
fix _should_fallback_to_positional
topper-123 34519f1
assert dtype
topper-123 ce35d54
NumericIndex._engine_type
topper-123 8583961
typing bug
topper-123 ce82b42
typing issues
topper-123 02f54b2
change according to comments
topper-123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,9 +30,13 @@ | |
from pandas.core.dtypes.common import ( | ||
is_datetime64_dtype, | ||
is_datetime64tz_dtype, | ||
is_float_dtype, | ||
is_integer_dtype, | ||
is_period_dtype, | ||
is_sequence, | ||
is_timedelta64_dtype, | ||
is_unsigned_integer_dtype, | ||
pandas_dtype, | ||
) | ||
|
||
import pandas as pd | ||
|
@@ -41,11 +45,14 @@ | |
CategoricalIndex, | ||
DataFrame, | ||
DatetimeIndex, | ||
Float64Index, | ||
Index, | ||
Int64Index, | ||
IntervalIndex, | ||
MultiIndex, | ||
RangeIndex, | ||
Series, | ||
UInt64Index, | ||
bdate_range, | ||
) | ||
from pandas._testing._io import ( # noqa:F401 | ||
|
@@ -292,21 +299,41 @@ def makeBoolIndex(k=10, name=None): | |
return Index([False, True] + [False] * (k - 2), name=name) | ||
|
||
|
||
def makeNumericIndex(k=10, name=None, *, dtype): | ||
dtype = pandas_dtype(dtype) | ||
assert isinstance(dtype, np.dtype) | ||
|
||
if is_integer_dtype(dtype): | ||
values = np.arange(k, dtype=dtype) | ||
if is_unsigned_integer_dtype(dtype): | ||
values += 2 ** (dtype.itemsize * 8 - 1) | ||
elif is_float_dtype(dtype): | ||
values = np.random.random_sample(k) - np.random.random_sample(1) | ||
values.sort() | ||
values = values * (10 ** np.random.randint(0, 9)) | ||
else: | ||
raise NotImplementedError(f"wrong dtype {dtype}") | ||
|
||
return Index(values, dtype=dtype, name=name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In #41153 we'll only need to change this to |
||
|
||
|
||
def makeIntIndex(k=10, name=None): | ||
return Index(list(range(k)), name=name) | ||
base_idx = makeNumericIndex(k, name=name, dtype="int64") | ||
return Int64Index(base_idx) | ||
|
||
|
||
def makeUIntIndex(k=10, name=None): | ||
return Index([2 ** 63 + i for i in range(k)], name=name) | ||
base_idx = makeNumericIndex(k, name=name, dtype="uint64") | ||
return UInt64Index(base_idx) | ||
|
||
|
||
def makeRangeIndex(k=10, name=None, **kwargs): | ||
return RangeIndex(0, k, 1, name=name, **kwargs) | ||
|
||
|
||
def makeFloatIndex(k=10, name=None): | ||
values = sorted(np.random.random_sample(k)) - np.random.random_sample(1) | ||
return Index(values * (10 ** np.random.randint(0, 9)), name=name) | ||
base_idx = makeNumericIndex(k, name=name, dtype="float64") | ||
return Float64Index(base_idx) | ||
|
||
|
||
def makeDateIndex(k: int = 10, freq="B", name=None, **kwargs) -> DatetimeIndex: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we expect non-numpy dtypes here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ve added an assert for that here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my idea was to just use
dtype = np.dtype(dtype)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I think we should hold on this until the above raises IMO.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will not ever raise as these are pandas dtypes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jbrockmendel raises a valid point; if u r expecting only numpy dtypes then why are pandas dtypes passed?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert isinstance(dtype, np.dtype)
on line 304 ensures that it will raise if it is a pandas dtype.I'm not intending to pass pandas dtypes here, but I'm using
pandas_dtype
to avoid an ambiguity: The string"Int64"
in Pandas meansInt64Dtype
, but in numpy it meansnp.int64
. So by usingdtype = pandas_dtype(dtype)
, then anassert(dtype, np.dtype)
, we ensure the correct dtype..