-
Notifications
You must be signed in to change notification settings - Fork 12
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
DM-43925: Add workarounds for pandas bugs when using non-floating-point masked columns. #998
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
906182e
Add astropy_to_pandas conversion that avoids pandas bugs.
erykoff 58cdb9d
Add option to set index when converting astropy to pandas.
erykoff 99f72af
Convert pandas to astropy via arrow to avoid pandas bugs.
erykoff 7a0f6d2
Allow string checking to handle empty rows.
erykoff 6b2e908
Add additional masked dataframe tests with large ints.
erykoff 9cb5298
Add changelog fragment.
erykoff e620aae
Remove unused import.
erykoff File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Work around pandas bugs when using non-floating-point masked columns. |
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
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
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 |
---|---|---|
|
@@ -96,10 +96,12 @@ | |
arrow_to_numpy_dict, | ||
arrow_to_pandas, | ||
astropy_to_arrow, | ||
astropy_to_pandas, | ||
compute_row_group_size, | ||
numpy_dict_to_arrow, | ||
numpy_to_arrow, | ||
pandas_to_arrow, | ||
pandas_to_astropy, | ||
) | ||
except ImportError: | ||
pa = None | ||
|
@@ -200,6 +202,7 @@ def _makeSingleIndexDataFrame(include_masked=False, include_lists=False): | |
df["m2"] = pd.array(np.arange(nrow), dtype=np.float32) | ||
df["mstrcol"] = pd.array(np.array(["text"] * nrow)) | ||
df.loc[1, ["m1", "m2", "mstrcol"]] = None | ||
df.loc[0, "m1"] = 1649900760361600113 | ||
|
||
if include_lists: | ||
nrow = len(df) | ||
|
@@ -273,6 +276,7 @@ def _makeSimpleAstropyTable(include_multidim=False, include_masked=False, includ | |
# Masked 64-bit integer. | ||
arr = np.arange(nrow, dtype="i8") | ||
arr[mask] = -1 | ||
arr[0] = 1649900760361600113 | ||
table["m_i8"] = np.ma.masked_array(data=arr, mask=mask, fill_value=-1) | ||
# Masked 32-bit float. | ||
arr = np.arange(nrow, dtype="f4") | ||
|
@@ -555,7 +559,7 @@ def testWriteSingleIndexDataFrameWithMaskedColsReadAsAstropyTable(self): | |
self.butler.put(df1, self.datasetType, dataId={}) | ||
|
||
tab2 = self.butler.get(self.datasetType, dataId={}, storageClass="ArrowAstropy") | ||
tab2_df = tab2.to_pandas(index="index") | ||
tab2_df = astropy_to_pandas(tab2, index="index") | ||
|
||
self.assertTrue(df1.columns.equals(tab2_df.columns)) | ||
for name in tab2_df.columns: | ||
|
@@ -584,6 +588,23 @@ def testWriteMultiIndexDataFrameReadAsAstropyTable(self): | |
# This test simply checks that it's readable, but definitely not | ||
# recommended. | ||
|
||
@unittest.skipUnless(atable is not None, "Cannot test writing as astropy without astropy.") | ||
def testWriteAstropyTableWithMaskedColsReadAsSingleIndexDataFrame(self): | ||
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. That is quite the mouthful... though I don't have a better suggestion. |
||
tab1 = _makeSimpleAstropyTable(include_masked=True) | ||
|
||
self.butler.put(tab1, self.datasetType, dataId={}) | ||
|
||
tab2 = self.butler.get(self.datasetType, dataId={}) | ||
|
||
tab1_df = astropy_to_pandas(tab1) | ||
self.assertTrue(tab1_df.equals(tab2)) | ||
|
||
tab2_astropy = pandas_to_astropy(tab2) | ||
for col in tab1.dtype.names: | ||
np.testing.assert_array_equal(tab2_astropy[col], tab1[col]) | ||
if isinstance(tab1[col], atable.column.MaskedColumn): | ||
np.testing.assert_array_equal(tab2_astropy[col].mask, tab1[col].mask) | ||
|
||
@unittest.skipUnless(pa is not None, "Cannot test reading as arrow without pyarrow.") | ||
def testWriteSingleIndexDataFrameReadAsArrowTable(self): | ||
df1, allColumns = _makeSingleIndexDataFrame() | ||
|
@@ -967,7 +988,7 @@ def testWriteAstropyWithMaskedColsReadAsDataFrame(self): | |
|
||
tab2 = self.butler.get(self.datasetType, dataId={}, storageClass="DataFrame") | ||
|
||
tab1_df = tab1.to_pandas() | ||
tab1_df = astropy_to_pandas(tab1) | ||
|
||
self.assertTrue(tab1_df.columns.equals(tab2.columns)) | ||
for name in tab2.columns: | ||
|
@@ -984,6 +1005,18 @@ def testWriteAstropyWithMaskedColsReadAsDataFrame(self): | |
else: | ||
self.assertTrue(col1.equals(col2)) | ||
|
||
@unittest.skipUnless(pd is not None, "Cannot test writing as a dataframe without pandas.") | ||
def testWriteSingleIndexDataFrameWithMaskedColsReadAsAstropyTable(self): | ||
df1, allColumns = _makeSingleIndexDataFrame(include_masked=True) | ||
|
||
self.butler.put(df1, self.datasetType, dataId={}) | ||
|
||
tab2 = self.butler.get(self.datasetType, dataId={}) | ||
|
||
df1_tab = pandas_to_astropy(df1) | ||
|
||
self._checkAstropyTableEquality(df1_tab, tab2) | ||
|
||
@unittest.skipUnless(np is not None, "Cannot test reading as numpy without numpy.") | ||
def testWriteAstropyReadAsNumpyTable(self): | ||
tab1 = _makeSimpleAstropyTable() | ||
|
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.
Should this raise if it's not a string or
None
? If not, what about justif index:
?