Skip to content

Commit 483bb2c

Browse files
author
Carlos Souza
committed
Doing adjustments as per @jreback requests
1 parent cc4c15b commit 483bb2c

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

pandas/sparse/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def _init_dict(self, data, index, columns, dtype=None):
142142

143143
sp_maker = lambda x: SparseArray(x, kind=self._default_kind,
144144
fill_value=self._default_fill_value,
145-
copy=True)
145+
copy=True, dtype=dtype)
146146
sdict = DataFrame()
147147
for k, v in compat.iteritems(data):
148148
if isinstance(v, Series):
@@ -159,7 +159,7 @@ def _init_dict(self, data, index, columns, dtype=None):
159159
v = [v.get(i, nan) for i in index]
160160

161161
v = sp_maker(v)
162-
sdict[k] = v.astype(np.dtype(dtype)) if dtype is not None else v
162+
sdict[k] = v
163163

164164
# TODO: figure out how to handle this case, all nan's?
165165
# add in any other columns we want to have (completeness)

pandas/tests/sparse/test_frame.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,18 @@ def test_constructor_nan_dataframe(self):
236236
dtype=float)
237237
tm.assert_sp_frame_equal(result, expected)
238238

239+
def test_type_coercion_at_construction(self):
240+
# GH 15682
241+
result = pd.SparseDataFrame(
242+
{'a': [1, 0, 0], 'b': [0, 1, 0], 'c': [0, 0, 1]}, dtype='uint8',
243+
default_fill_value=0)
244+
expected = pd.SparseDataFrame(
245+
{'a': pd.SparseSeries([1, 0, 0], dtype='uint8'),
246+
'b': pd.SparseSeries([0, 1, 0], dtype='uint8'),
247+
'c': pd.SparseSeries([0, 0, 1], dtype='uint8')},
248+
default_fill_value=0)
249+
tm.assert_sp_frame_equal(result, expected)
250+
239251
def test_dtypes(self):
240252
df = DataFrame(np.random.randn(10000, 4))
241253
df.loc[:9998] = np.nan
@@ -756,8 +768,8 @@ def test_sparse_frame_fillna_limit(self):
756768

757769
def test_rename(self):
758770
# just check this works
759-
renamed = self.frame.rename(index=str) # noqa
760-
renamed = self.frame.rename(
771+
rename = self.frame.rename(index=str) # noqa
772+
rename = self.frame.rename(
761773
columns=lambda x: '%s%d' % (x, len(x))) # noqa
762774

763775
def test_corr(self):
@@ -1225,6 +1237,7 @@ def test_from_to_scipy_object(spmatrix, fill_value):
12251237

12261238

12271239
class TestSparseDataFrameArithmetic(tm.TestCase):
1240+
12281241
def test_numeric_op_scalar(self):
12291242
df = pd.DataFrame({'A': [nan, nan, 0, 1, ],
12301243
'B': [0, 1, 2, nan],
@@ -1297,11 +1310,3 @@ def test_numpy_func_call(self):
12971310
for func in funcs:
12981311
getattr(np, func)(self.frame)
12991312

1300-
def test_type_coercion_at_construction(self):
1301-
# GH 15682
1302-
df = pd.SparseDataFrame(
1303-
{'a': [1, 0, 0], 'b': [0, 1, 0], 'c': [0, 0, 1]}, dtype='uint8',
1304-
default_fill_value=0)
1305-
result = df.dtypes[0]
1306-
expected = np.dtype('uint8')
1307-
assert result == expected

0 commit comments

Comments
 (0)