Skip to content

Commit 4389fdd

Browse files
committed
COMPAT: DataFrame construction compat with xarray.Dataset
closes pandas-dev#12353
1 parent 2e9a774 commit 4389fdd

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

pandas/core/frame.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
is_sequence, _infer_dtype_from_scalar, _values_from_object, is_list_like,
2929
_maybe_box_datetimelike, is_categorical_dtype, is_object_dtype,
3030
is_internal_type, is_datetimetz, _possibly_infer_to_datetimelike,
31-
_dict_compat)
31+
_dict_compat, is_dict_like)
3232
from pandas.core.generic import NDFrame, _shared_docs
3333
from pandas.core.index import Index, MultiIndex, _ensure_index
3434
from pandas.core.indexing import (maybe_droplevels, convert_to_index_sliceable,
@@ -219,8 +219,6 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
219219
if isinstance(data, BlockManager):
220220
mgr = self._init_mgr(data, axes=dict(index=index, columns=columns),
221221
dtype=dtype, copy=copy)
222-
elif isinstance(data, dict):
223-
mgr = self._init_dict(data, index, columns, dtype=dtype)
224222
elif isinstance(data, ma.MaskedArray):
225223
import numpy.ma.mrecords as mrecords
226224
# masked recarray
@@ -252,6 +250,8 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
252250
else:
253251
mgr = self._init_ndarray(data, index, columns, dtype=dtype,
254252
copy=copy)
253+
elif is_dict_like(data):
254+
mgr = self._init_dict(data, index, columns, dtype=dtype)
255255
elif isinstance(data, (list, types.GeneratorType)):
256256
if isinstance(data, types.GeneratorType):
257257
data = list(data)

pandas/core/panel.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
from pandas.compat import (map, zip, range, u, OrderedDict, OrderedDefaultdict)
1717
from pandas.core.categorical import Categorical
1818
from pandas.core.common import (PandasError, _try_sort, _default_index,
19-
_infer_dtype_from_scalar, is_list_like)
19+
_infer_dtype_from_scalar, is_list_like,
20+
is_dict_like)
2021
from pandas.core.frame import DataFrame
2122
from pandas.core.generic import NDFrame, _shared_docs
2223
from pandas.core.index import (Index, MultiIndex, _ensure_index,
@@ -157,7 +158,7 @@ def _init_data(self, data, copy, dtype, **kwargs):
157158
axes = [x if x is not None else y
158159
for x, y in zip(passed_axes, data.axes)]
159160
mgr = data
160-
elif isinstance(data, dict):
161+
elif is_dict_like(data):
161162
mgr = self._init_dict(data, passed_axes, dtype=dtype)
162163
copy = False
163164
dtype = None

pandas/tests/test_generic.py

+8
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,8 @@ def testit(index, check_index_type=True):
10341034
self.assertIsInstance(result, DataArray)
10351035
assert_series_equal(result.to_series(), s)
10361036

1037+
#assert_series_equal(Series(result), s)
1038+
10371039

10381040
class TestDataFrame(tm.TestCase, Generic):
10391041
_typ = DataFrame
@@ -1832,6 +1834,10 @@ def test_to_xarray(self):
18321834
expected,
18331835
check_index_type=False)
18341836

1837+
assert_frame_equal(DataFrame(result).set_index('foo'),
1838+
expected,
1839+
check_index_type=False)
1840+
18351841
# not implemented
18361842
df.index = pd.MultiIndex.from_product([['a'], range(3)],
18371843
names=['one', 'two'])
@@ -1859,6 +1865,8 @@ def test_to_xarray(self):
18591865
# idempotency
18601866
assert_panel_equal(result.to_pandas(), p)
18611867

1868+
#assert_panel_equal(Panel(result), p)
1869+
18621870

18631871
class TestPanel4D(tm.TestCase, Generic):
18641872
_typ = Panel4D

0 commit comments

Comments
 (0)