-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Sparse creation with object dtype may raise TypeError #13201
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
Closed
Closed
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -152,9 +152,17 @@ def __new__(cls, data, sparse_index=None, index=None, kind='integer', | |
|
||
# Create array, do *not* copy data by default | ||
if copy: | ||
subarr = np.array(values, dtype=dtype, copy=True) | ||
try: | ||
# ToDo: Can remove this error handling when we actually | ||
# support other dtypes | ||
subarr = np.array(values, dtype=dtype, copy=True) | ||
except ValueError: | ||
subarr = np.array(values, copy=True) | ||
else: | ||
subarr = np.asarray(values, dtype=dtype) | ||
try: | ||
subarr = np.asarray(values, dtype=dtype) | ||
except ValueError: | ||
subarr = np.asarray(values) | ||
|
||
# if we have a bool type, make sure that we have a bool fill_value | ||
if ((dtype is not None and issubclass(dtype.type, np.bool_)) or | ||
|
@@ -437,12 +445,12 @@ def count(self): | |
|
||
@property | ||
def _null_fill_value(self): | ||
return np.isnan(self.fill_value) | ||
return com.isnull(self.fill_value) | ||
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. import |
||
|
||
@property | ||
def _valid_sp_values(self): | ||
sp_vals = self.sp_values | ||
mask = np.isfinite(sp_vals) | ||
mask = com.notnull(sp_vals) | ||
return sp_vals[mask] | ||
|
||
@Appender(_index_shared_docs['fillna'] % _sparray_doc_kwargs) | ||
|
@@ -616,8 +624,8 @@ def make_sparse(arr, kind='block', fill_value=nan): | |
if arr.ndim > 1: | ||
raise TypeError("expected dimension <= 1 data") | ||
|
||
if np.isnan(fill_value): | ||
mask = ~np.isnan(arr) | ||
if com.isnull(fill_value): | ||
mask = com.notnull(arr) | ||
else: | ||
mask = arr != fill_value | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# -*- coding: utf-8 -*- | ||
import numpy as np | ||
import pandas as pd | ||
import pandas.util.testing as tm | ||
|
||
|
||
class TestSparseGroupBy(tm.TestCase): | ||
|
||
_multiprocess_can_split_ = True | ||
|
||
def setUp(self): | ||
self.dense = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar', | ||
'foo', 'bar', 'foo', 'foo'], | ||
'B': ['one', 'one', 'two', 'three', | ||
'two', 'two', 'one', 'three'], | ||
'C': np.random.randn(8), | ||
'D': np.random.randn(8), | ||
'E': [np.nan, np.nan, 1, 2, | ||
np.nan, 1, np.nan, np.nan]}) | ||
self.sparse = self.dense.to_sparse() | ||
|
||
def test_first_last_nth(self): | ||
# tests for first / last / nth | ||
sparse_grouped = self.sparse.groupby('A') | ||
dense_grouped = self.dense.groupby('A') | ||
|
||
tm.assert_frame_equal(sparse_grouped.first(), | ||
dense_grouped.first()) | ||
tm.assert_frame_equal(sparse_grouped.last(), | ||
dense_grouped.last()) | ||
tm.assert_frame_equal(sparse_grouped.nth(1), | ||
dense_grouped.nth(1)) | ||
|
||
def test_aggfuncs(self): | ||
sparse_grouped = self.sparse.groupby('A') | ||
dense_grouped = self.dense.groupby('A') | ||
|
||
tm.assert_frame_equal(sparse_grouped.mean(), | ||
dense_grouped.mean()) | ||
|
||
# ToDo: sparse sum includes str column | ||
# tm.assert_frame_equal(sparse_grouped.sum(), | ||
# dense_grouped.sum()) | ||
|
||
tm.assert_frame_equal(sparse_grouped.count(), | ||
dense_grouped.count()) |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import pandas.util.testing as tm | ||
|
||
|
||
class TestPivotTable(tm.TestCase): | ||
|
||
_multiprocess_can_split_ = True | ||
|
||
def setUp(self): | ||
self.dense = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar', | ||
'foo', 'bar', 'foo', 'foo'], | ||
'B': ['one', 'one', 'two', 'three', | ||
'two', 'two', 'one', 'three'], | ||
'C': np.random.randn(8), | ||
'D': np.random.randn(8), | ||
'E': [np.nan, np.nan, 1, 2, | ||
np.nan, 1, np.nan, np.nan]}) | ||
self.sparse = self.dense.to_sparse() | ||
|
||
def test_pivot_table(self): | ||
res_sparse = pd.pivot_table(self.sparse, index='A', columns='B', | ||
values='C') | ||
res_dense = pd.pivot_table(self.dense, index='A', columns='B', | ||
values='C') | ||
tm.assert_frame_equal(res_sparse, res_dense) | ||
|
||
res_sparse = pd.pivot_table(self.sparse, index='A', columns='B', | ||
values='E') | ||
res_dense = pd.pivot_table(self.dense, index='A', columns='B', | ||
values='E') | ||
tm.assert_frame_equal(res_sparse, res_dense) | ||
|
||
res_sparse = pd.pivot_table(self.sparse, index='A', columns='B', | ||
values='E', aggfunc='mean') | ||
res_dense = pd.pivot_table(self.dense, index='A', columns='B', | ||
values='E', aggfunc='mean') | ||
tm.assert_frame_equal(res_sparse, res_dense) | ||
|
||
# ToDo: sum doesn't handle nan properly | ||
# res_sparse = pd.pivot_table(self.sparse, index='A', columns='B', | ||
# values='E', aggfunc='sum') | ||
# res_dense = pd.pivot_table(self.dense, index='A', columns='B', | ||
# values='E', aggfunc='sum') | ||
# tm.assert_frame_equal(res_sparse, res_dense) | ||
|
||
def test_pivot_table_multi(self): | ||
res_sparse = pd.pivot_table(self.sparse, index='A', columns='B', | ||
values=['D', 'E']) | ||
res_dense = pd.pivot_table(self.dense, index='A', columns='B', | ||
values=['D', 'E']) | ||
tm.assert_frame_equal(res_sparse, res_dense) |
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.
hmm, maybe this should be more integrated with
_sanitize_values
(or that should be enhanced). not a big deal, but feel free to reorg code to make more general / readable code.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.
Can I leave this ATM, because this can be removed entirely after changing
dtype
default toNone
(#667).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.
yes - was just bringing it up mainly for future / FYI