-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
COMPAT: Objects construction compat with xarray.Dataset #12400
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 |
---|---|---|
|
@@ -54,7 +54,8 @@ | |
is_list_like, | ||
is_iterator, | ||
is_sequence, | ||
is_named_tuple) | ||
is_named_tuple, | ||
is_dict_like) | ||
from pandas.types.missing import isnull, notnull | ||
|
||
from pandas.core.common import (PandasError, _try_sort, | ||
|
@@ -64,11 +65,11 @@ | |
_dict_compat) | ||
from pandas.core.generic import NDFrame, _shared_docs | ||
from pandas.core.index import Index, MultiIndex, _ensure_index | ||
from pandas.core.indexing import (maybe_droplevels, convert_to_index_sliceable, | ||
check_bool_indexer) | ||
from pandas.core.internals import (BlockManager, | ||
create_block_manager_from_arrays, | ||
create_block_manager_from_blocks) | ||
from pandas.core.indexing import ( | ||
maybe_droplevels, convert_to_index_sliceable, check_bool_indexer) | ||
from pandas.core.internals import ( | ||
BlockManager, create_block_manager_from_arrays, | ||
create_block_manager_from_blocks) | ||
from pandas.core.series import Series | ||
from pandas.core.categorical import Categorical | ||
import pandas.computation.expressions as expressions | ||
|
@@ -259,11 +260,16 @@ def __init__(self, data=None, index=None, columns=None, dtype=None, | |
if isinstance(data, DataFrame): | ||
data = data._data | ||
|
||
if hasattr(data, 'to_dataframe'): # xr.Dataset | ||
if index or columns or dtype or copy: | ||
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. could be an |
||
raise ValueError("Supply only a Dataset if supplying a " | ||
"Dataset") | ||
data = data.to_dataframe()._data | ||
|
||
if isinstance(data, BlockManager): | ||
mgr = self._init_mgr(data, axes=dict(index=index, columns=columns), | ||
dtype=dtype, copy=copy) | ||
elif isinstance(data, dict): | ||
mgr = self._init_dict(data, index, columns, dtype=dtype) | ||
|
||
elif isinstance(data, ma.MaskedArray): | ||
import numpy.ma.mrecords as mrecords | ||
# masked recarray | ||
|
@@ -295,6 +301,8 @@ def __init__(self, data=None, index=None, columns=None, dtype=None, | |
else: | ||
mgr = self._init_ndarray(data, index, columns, dtype=dtype, | ||
copy=copy) | ||
elif is_dict_like(data): | ||
mgr = self._init_dict(data, index, columns, dtype=dtype) | ||
elif isinstance(data, (list, types.GeneratorType)): | ||
if isinstance(data, types.GeneratorType): | ||
data = list(data) | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -596,6 +596,15 @@ def test_constructor_subclass_dict(self): | |
refseries = Series(dict(compat.iteritems(data))) | ||
assert_series_equal(refseries, series) | ||
|
||
def test_constructor_mapping(self): | ||
|
||
mapping = tm.MappingMock(base=2) | ||
|
||
result = Series(mapping) | ||
expected = pd.Series([8, 10], index=[4, 5]) | ||
|
||
assert_series_equal(result, expected) | ||
|
||
def test_constructor_dict_datetime64_index(self): | ||
# GH 9456 | ||
|
||
|
@@ -769,6 +778,27 @@ def f(): | |
s = Series([pd.NaT, np.nan, '1 Day']) | ||
self.assertEqual(s.dtype, 'timedelta64[ns]') | ||
|
||
def test_constructor_dict_numpy_0d_arrays(self): | ||
|
||
data = [np.asarray(i) for i in range(4)] | ||
|
||
result = Series(data) | ||
expected = Series(range(4)) | ||
|
||
# disabled for the moment (will remove from PR) | ||
# assert_series_equal(result, expected) | ||
|
||
def test_constructor_xarray_dataset(self): | ||
tm._skip_if_no_xarray() | ||
import xarray as xr | ||
|
||
d = {'a': 5, 'b': 10} | ||
result = Series(xr.Dataset(d)) | ||
expected = Series(d) | ||
|
||
# disabled for the moment (will remove from PR) | ||
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. Because of the issues with 0-d arrays discussed in the issue |
||
# assert_series_equal(result, expected) | ||
|
||
def test_constructor_name_hashable(self): | ||
for n in [777, 777., 'name', datetime(2001, 11, 11), (1, ), u"\u05D0"]: | ||
for data in [[1, 2, 3], np.ones(3), {'a': 0, 'b': 1}]: | ||
|
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.
I would slightly rather skip this special check. In theory,
to_dataframe
might return something else.I don't think it will be that much slower to use the generic dict path, though we then run into the issue that
DataFrame._init_dict
does a special check for OrderedDict when deciding whether or not to order the keys. Sadly there's no way in check whether an arbitrary Mapping type has intentionally ordered keys or not (Python-ideas discussed addingcollections.abc.Ordered
but I don't think it was implemented).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.
The broader question is whether we want objects to be able to define their own conversion to a
DataFrame
- I think the main options are:dict
DataFrame
constructor - i.e. thatif
statement checks forDataSet
(and anything else we want to check)to_dataframe
that classes can define themselvesThoughts?
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'm happy with a duck-like method, but we should probably call it something very explicit like
_to_pandas_dataframe_
. This would be useful for a lot of other projects, not just xarray.