Skip to content

BUG: preserve empty dtype if possible when creating an empty BlockManager #4677

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

Merged
merged 1 commit into from
Aug 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -1582,7 +1582,10 @@ def make_empty(self, axes=None):
""" return an empty BlockManager with the items axis of len 0 """
if axes is None:
axes = [_ensure_index([]) ] + [ _ensure_index(a) for a in self.axes[1:] ]
return self.__class__(np.array([]), axes)

# preserve dtype if possible
dtype = self.dtype if self.ndim == 1 else object
return self.__class__(np.array([],dtype=dtype), axes)

def __nonzero__(self):
return True
Expand Down
33 changes: 33 additions & 0 deletions pandas/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,31 @@ def test_rename_mi(self):
index=MultiIndex.from_tuples([("A",x) for x in ["a","B","c"]]))
result = s.rename(str.lower)

def test_get_numeric_data_preserve_dtype(self):

# get the numeric data
o = Series([1,2,3])
result = o._get_numeric_data()
self._compare(result, o)

o = Series([1,'2',3.])
result = o._get_numeric_data()
expected = Series([],dtype=object)
self._compare(result, expected)

o = Series([True,False,True])
result = o._get_numeric_data()
self._compare(result, o)

o = Series([True,False,True])
result = o._get_bool_data()
self._compare(result, o)

o = Series(date_range('20130101',periods=3))
result = o._get_numeric_data()
expected = Series([],dtype='M8[ns]')
self._compare(result, expected)

class TestDataFrame(unittest.TestCase, Generic):
_typ = DataFrame
_comparator = lambda self, x, y: assert_frame_equal(x,y)
Expand All @@ -138,6 +163,14 @@ def test_rename_mi(self):
index=MultiIndex.from_tuples([("A",x) for x in ["a","B","c"]]))
result = df.rename(str.lower)

def test_get_numeric_data_preserve_dtype(self):

# get the numeric data
o = DataFrame({'A' : [1,'2',3.] })
result = o._get_numeric_data()
expected = DataFrame(index=[0,1,2],dtype=object)
self._compare(result, expected)

class TestPanel(unittest.TestCase, Generic):
_typ = Panel
_comparator = lambda self, x, y: assert_panel_equal(x,y)
Expand Down