-
Notifications
You must be signed in to change notification settings - Fork 654
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
FEAT-#5836: Introduce 'partial' dtypes cache #6663
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
068453d
FEAT-#5836: Introduce 'partial' dtypes cache
dchigarev ac17c16
fixes
dchigarev 84fc9f1
revert fix for 6732
dchigarev 0b644fa
add more tests
dchigarev e89d7b6
Merge remote-tracking branch 'origin' into smart_dtypes
dchigarev 64049a3
Apply review suggestions
dchigarev 6cd1472
Merge remote-tracking branch 'origin/master' into smart_dtypes
dchigarev 24b75c0
apply suggestions 2
dchigarev eeb7721
fix how we catch exceptions
dchigarev 7469611
np.unique -> set
dchigarev 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 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 |
---|---|---|
|
@@ -36,6 +36,7 @@ | |
lazy_metadata_decorator, | ||
) | ||
from modin.core.dataframe.pandas.metadata import ( | ||
DtypesDescriptor, | ||
LazyProxyCategoricalDtype, | ||
ModinDtypes, | ||
ModinIndex, | ||
|
@@ -314,25 +315,39 @@ | |
new_parent : object, optional | ||
A new parent to link the proxies to. If not specified | ||
will consider the `self` to be a new parent. | ||
|
||
Returns | ||
------- | ||
pandas.Series, ModinDtypes or callable | ||
""" | ||
new_parent = new_parent or self | ||
if isinstance(dtypes, pandas.Series) or ( | ||
isinstance(dtypes, ModinDtypes) and dtypes.is_materialized | ||
): | ||
for key, value in dtypes.items(): | ||
if isinstance(value, LazyProxyCategoricalDtype): | ||
dtypes[key] = value._update_proxy(new_parent, column_name=key) | ||
if isinstance(dtypes, ModinDtypes): | ||
dtypes = dtypes.maybe_specify_new_frame_ref(new_parent) | ||
if isinstance(dtypes, pandas.Series): | ||
LazyProxyCategoricalDtype.update_dtypes(dtypes, new_parent) | ||
return dtypes | ||
|
||
def set_dtypes_cache(self, dtypes): | ||
""" | ||
Set dtypes cache. | ||
|
||
Parameters | ||
---------- | ||
dtypes : pandas.Series, ModinDtypes or callable | ||
""" | ||
self._maybe_update_proxies(dtypes) | ||
if isinstance(dtypes, ModinDtypes) or dtypes is None: | ||
dtypes : pandas.Series, ModinDtypes, callable or None | ||
""" | ||
dtypes = self._maybe_update_proxies(dtypes) | ||
if dtypes is None and self.has_materialized_columns: | ||
# try to set a descriptor instead of 'None' to be more flexible in | ||
# dtypes computing | ||
try: | ||
self._dtypes = ModinDtypes( | ||
DtypesDescriptor( | ||
cols_with_unknown_dtypes=self.columns.tolist(), parent_df=self | ||
) | ||
) | ||
except NotImplementedError: | ||
self._dtypes = None | ||
elif isinstance(dtypes, ModinDtypes) or dtypes is None: | ||
self._dtypes = dtypes | ||
else: | ||
self._dtypes = ModinDtypes(dtypes) | ||
|
@@ -354,6 +369,18 @@ | |
self.set_dtypes_cache(dtypes) | ||
return dtypes | ||
|
||
def get_dtypes_set(self): | ||
""" | ||
Get a set of dtypes that are in this dataframe. | ||
|
||
Returns | ||
------- | ||
set | ||
""" | ||
if isinstance(self._dtypes, ModinDtypes): | ||
return self._dtypes.get_dtypes_set() | ||
return set(self.dtypes.values) | ||
|
||
def _compute_dtypes(self, columns=None): | ||
""" | ||
Compute the data types via TreeReduce pattern for the specified columns. | ||
|
@@ -376,7 +403,13 @@ | |
if columns is not None: | ||
# Sorting positions to request columns in the order they're stored (it's more efficient) | ||
numeric_indices = sorted(self.columns.get_indexer_for(columns)) | ||
obj = self._take_2d_positional(col_positions=numeric_indices) | ||
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.
|
||
if any(pos < 0 for pos in numeric_indices): | ||
raise KeyError( | ||
f"Some of the columns are not in index: subset={columns}; columns={self.columns}" | ||
) | ||
obj = self.take_2d_labels_or_positional( | ||
col_labels=self.columns[numeric_indices].tolist() | ||
) | ||
else: | ||
obj = self | ||
|
||
|
@@ -675,8 +708,11 @@ | |
): | ||
return | ||
new_columns = self._validate_set_axis(new_columns, self._columns_cache) | ||
if self.has_materialized_dtypes: | ||
self.dtypes.index = new_columns | ||
if isinstance(self._dtypes, ModinDtypes): | ||
new_value = self._dtypes.set_index(new_columns) | ||
self.set_dtypes_cache(new_value) | ||
elif isinstance(self._dtypes, pandas.Series): | ||
self.dtypes.index = new_columns | ||
self.set_columns_cache(new_columns) | ||
self.synchronize_labels(axis=1) | ||
|
||
|
@@ -1146,6 +1182,14 @@ | |
|
||
if self.has_materialized_dtypes: | ||
new_dtypes = self.dtypes.iloc[monotonic_col_idx] | ||
elif isinstance(self._dtypes, ModinDtypes): | ||
try: | ||
new_dtypes = self._dtypes.lazy_get( | ||
monotonic_col_idx, numeric_index=True | ||
) | ||
# can raise either on missing cache or on duplicated labels | ||
except (ValueError, NotImplementedError): | ||
new_dtypes = None | ||
else: | ||
new_dtypes = None | ||
else: | ||
|
@@ -1441,6 +1485,12 @@ | |
col_idx = self.columns[col_positions] | ||
if self.has_materialized_dtypes: | ||
new_dtypes = self.dtypes.iloc[col_positions] | ||
elif isinstance(self._dtypes, ModinDtypes): | ||
try: | ||
new_dtypes = self._dtypes.lazy_get(col_idx) | ||
# can raise on duplicated labels | ||
except NotImplementedError: | ||
new_dtypes = None | ||
|
||
if len(col_idx) != len(self.columns): | ||
# The frame was re-partitioned along the 1 axis during reordering using | ||
|
@@ -3253,22 +3303,24 @@ | |
kw = {"row_lengths": None, "column_widths": None} | ||
if isinstance(dtypes, str) and dtypes == "copy": | ||
kw["dtypes"] = self.copy_dtypes_cache() | ||
elif isinstance(dtypes, DtypesDescriptor): | ||
kw["dtypes"] = ModinDtypes(dtypes) | ||
elif dtypes is not None: | ||
if isinstance(dtypes, (pandas.Series, ModinDtypes)): | ||
kw["dtypes"] = dtypes.copy() | ||
else: | ||
if new_columns is None: | ||
( | ||
new_columns, | ||
kw["column_widths"], | ||
) = self._compute_axis_labels_and_lengths(1, new_partitions) | ||
kw["dtypes"] = ( | ||
pandas.Series(dtypes, index=new_columns) | ||
if is_list_like(dtypes) | ||
else pandas.Series( | ||
[np.dtype(dtypes)] * len(new_columns), index=new_columns | ||
kw["dtypes"] = ModinDtypes( | ||
DtypesDescriptor(remaining_dtype=np.dtype(dtypes)) | ||
) | ||
else: | ||
kw["dtypes"] = ( | ||
pandas.Series(dtypes, index=new_columns) | ||
if is_list_like(dtypes) | ||
else pandas.Series( | ||
[np.dtype(dtypes)] * len(new_columns), index=new_columns | ||
) | ||
) | ||
) | ||
|
||
if not keep_partitioning: | ||
if kw["row_lengths"] is None and new_index is not None: | ||
|
@@ -3662,10 +3714,12 @@ | |
if all(obj.has_materialized_columns for obj in (self, *others)): | ||
new_columns = self.columns.append([other.columns for other in others]) | ||
new_index = joined_index | ||
if self.has_materialized_dtypes and all( | ||
o.has_materialized_dtypes for o in others | ||
): | ||
new_dtypes = pandas.concat([self.dtypes] + [o.dtypes for o in others]) | ||
try: | ||
new_dtypes = ModinDtypes.concat( | ||
[self.copy_dtypes_cache()] + [o.copy_dtypes_cache() for o in others] | ||
) | ||
except NotImplementedError: | ||
|
||
new_dtypes = None | ||
# If we have already cached the width of each column in at least one | ||
# of the column's partitions, we can build new_widths for the new | ||
# frame. Typically, if we know the width for any partition in a | ||
|
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
return updated value for convenience