Skip to content

Commit 057d56b

Browse files
committed
Wording and code organization fixes.
1 parent 926ca1e commit 057d56b

File tree

4 files changed

+109
-125
lines changed

4 files changed

+109
-125
lines changed

doc/source/whatsnew/v0.19.0.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ API changes
437437
- ``pd.Timedelta(None)`` is now accepted and will return ``NaT``, mirroring ``pd.Timestamp`` (:issue:`13687`)
438438
- ``Timestamp``, ``Period``, ``DatetimeIndex``, ``PeriodIndex`` and ``.dt`` accessor have gained a ``.is_leap_year`` property to check whether the date belongs to a leap year. (:issue:`13727`)
439439
- ``pd.read_hdf`` will now raise a ``ValueError`` instead of ``KeyError``, if a mode other than ``r``, ``r+`` and ``a`` is supplied. (:issue:`13623`)
440-
- ``.values`` will now return ``np.float64`` with a ``DataFrame`` with ``np.int64`` and ``np.uint64`` dtypes, conforming to ``np.find_common_type`` (:issue:`10364`, :issue:`13917`)
440+
- ``DataFrame.values`` will now return ``float64`` with a ``DataFrame`` of mixed ``int64`` and ``uint64`` dtypes, conforming to ``np.find_common_type`` (:issue:`10364`, :issue:`13917`)
441441

442442

443443

@@ -765,7 +765,7 @@ Note that the limitation is applied to ``fill_value`` which default is ``np.nan`
765765
- Bug in ``SparseDataFrame`` doesn't respect passed ``SparseArray`` or ``SparseSeries`` 's dtype and ``fill_value`` (:issue:`13866`)
766766
- Bug in ``SparseArray`` and ``SparseSeries`` don't apply ufunc to ``fill_value`` (:issue:`13853`)
767767
- Bug in ``SparseSeries.abs`` incorrectly keeps negative ``fill_value`` (:issue:`13853`)
768-
- Bug when interacting with multi-type SparseDataFrames: single row slicing now works because types are not forced to float (:issue:`13917`)
768+
- Bug in single row slicing on multi-type ``SparseDataFrame``s: types were previously forced to float (:issue:`13917`)
769769

770770
.. _whatsnew_0190.deprecations:
771771

pandas/sparse/tests/test_indexing.py

+78
Original file line numberDiff line numberDiff line change
@@ -829,3 +829,81 @@ def test_reindex_fill_value(self):
829829
res = sparse.reindex(['A', 'C', 'B'])
830830
exp = orig.reindex(['A', 'C', 'B']).to_sparse(fill_value=0)
831831
tm.assert_sp_frame_equal(res, exp)
832+
833+
834+
class TestMultitype(tm.TestCase):
835+
def setUp(self):
836+
self.cols = ['string', 'int', 'float', 'object']
837+
838+
self.string_series = pd.SparseSeries(['a', 'b', 'c'])
839+
self.int_series = pd.SparseSeries([1, 2, 3])
840+
self.float_series = pd.SparseSeries([1.1, 1.2, 1.3])
841+
self.object_series = pd.SparseSeries([[], {}, set()])
842+
self.sdf = pd.SparseDataFrame({
843+
'string': self.string_series,
844+
'int': self.int_series,
845+
'float': self.float_series,
846+
'object': self.object_series,
847+
})
848+
self.sdf = self.sdf[self.cols]
849+
self.ss = pd.SparseSeries(['a', 1, 1.1, []], index=self.cols)
850+
851+
def test_frame_basic_dtypes(self):
852+
for _, row in self.sdf.iterrows():
853+
self.assertEqual(row.dtype, object)
854+
tm.assert_sp_series_equal(self.sdf['string'], self.string_series,
855+
check_names=False)
856+
tm.assert_sp_series_equal(self.sdf['int'], self.int_series,
857+
check_names=False)
858+
tm.assert_sp_series_equal(self.sdf['float'], self.float_series,
859+
check_names=False)
860+
tm.assert_sp_series_equal(self.sdf['object'], self.object_series,
861+
check_names=False)
862+
863+
def test_frame_indexing_single(self):
864+
tm.assert_sp_series_equal(self.sdf.iloc[0],
865+
pd.SparseSeries(['a', 1, 1.1, []],
866+
index=self.cols),
867+
check_names=False)
868+
tm.assert_sp_series_equal(self.sdf.iloc[1],
869+
pd.SparseSeries(['b', 2, 1.2, {}],
870+
index=self.cols),
871+
check_names=False)
872+
tm.assert_sp_series_equal(self.sdf.iloc[2],
873+
pd.SparseSeries(['c', 3, 1.3, set()],
874+
index=self.cols),
875+
check_names=False)
876+
877+
def test_frame_indexing_multiple(self):
878+
tm.assert_sp_frame_equal(self.sdf, self.sdf[:])
879+
tm.assert_sp_frame_equal(self.sdf, self.sdf.loc[:])
880+
tm.assert_sp_frame_equal(self.sdf.iloc[[1, 2]],
881+
pd.SparseDataFrame({
882+
'string': self.string_series.iloc[[1, 2]],
883+
'int': self.int_series.iloc[[1, 2]],
884+
'float': self.float_series.iloc[[1, 2]],
885+
'object': self.object_series.iloc[[1, 2]]
886+
}, index=[1, 2])[self.cols])
887+
tm.assert_sp_frame_equal(self.sdf[['int', 'string']],
888+
pd.SparseDataFrame({
889+
'int': self.int_series,
890+
'string': self.string_series,
891+
}))
892+
893+
def test_series_indexing_single(self):
894+
for i, idx in enumerate(self.cols):
895+
self.assertEqual(self.ss.iloc[i], self.ss[idx])
896+
self.assertEqual(type(self.ss.iloc[i]),
897+
type(self.ss[idx]))
898+
self.assertEqual(self.ss['string'], 'a')
899+
self.assertEqual(self.ss['int'], 1)
900+
self.assertEqual(self.ss['float'], 1.1)
901+
self.assertEqual(self.ss['object'], [])
902+
903+
def test_series_indexing_multiple(self):
904+
tm.assert_sp_series_equal(self.ss.loc[['string', 'int']],
905+
pd.SparseSeries(['a', 1],
906+
index=['string', 'int']))
907+
tm.assert_sp_series_equal(self.ss.loc[['string', 'object']],
908+
pd.SparseSeries(['a', []],
909+
index=['string', 'object']))

pandas/sparse/tests/test_multitype.py

-88
This file was deleted.

pandas/tests/types/test_cast.py

+29-35
Original file line numberDiff line numberDiff line change
@@ -193,43 +193,37 @@ def test_possibly_convert_objects_copy(self):
193193

194194
class TestCommonTypes(tm.TestCase):
195195
def test_numpy_dtypes(self):
196-
# identity
197-
self.assertEqual(_find_common_type([np.int64]), np.int64)
198-
self.assertEqual(_find_common_type([np.uint64]), np.uint64)
199-
self.assertEqual(_find_common_type([np.float32]), np.float32)
200-
self.assertEqual(_find_common_type([np.object]), np.object)
201-
202-
# into ints
203-
self.assertEqual(_find_common_type([np.int16, np.int64]),
204-
np.int64)
205-
self.assertEqual(_find_common_type([np.int32, np.uint32]),
206-
np.int64)
207-
self.assertEqual(_find_common_type([np.uint16, np.uint64]),
208-
np.uint64)
209-
210-
# into floats
211-
self.assertEqual(_find_common_type([np.float16, np.float32]),
212-
np.float32)
213-
self.assertEqual(_find_common_type([np.float16, np.int16]),
214-
np.float32)
215-
self.assertEqual(_find_common_type([np.float32, np.int16]),
216-
np.float32)
217-
self.assertEqual(_find_common_type([np.uint64, np.int64]),
218-
np.float64)
219-
self.assertEqual(_find_common_type([np.int16, np.float64]),
220-
np.float64)
221-
self.assertEqual(_find_common_type([np.float16, np.int64]),
222-
np.float64)
223-
224-
# into others
225-
self.assertEqual(_find_common_type([np.complex128, np.int32]),
226-
np.complex128)
227-
self.assertEqual(_find_common_type([np.object, np.float32]),
228-
np.object)
229-
self.assertEqual(_find_common_type([np.object, np.int16]),
230-
np.object)
196+
# (source_types, destination_type)
197+
testcases = (
198+
# identity
199+
((np.int64,), np.int64),
200+
((np.uint64,), np.uint64),
201+
((np.float32,), np.float32),
202+
((np.object,), np.object),
203+
204+
# into ints
205+
((np.int16, np.int64), np.int64),
206+
((np.int32, np.uint32), np.int64),
207+
((np.uint16, np.uint64), np.uint64),
208+
209+
# into floats
210+
((np.float16, np.float32), np.float32),
211+
((np.float16, np.int16), np.float32),
212+
((np.float32, np.int16), np.float32),
213+
((np.uint64, np.int64), np.float64),
214+
((np.int16, np.float64), np.float64),
215+
((np.float16, np.int64), np.float64),
216+
217+
# into others
218+
((np.complex128, np.int32), np.complex128),
219+
((np.object, np.float32), np.object),
220+
((np.object, np.int16), np.object),
221+
)
222+
for src, common in testcases:
223+
self.assertEqual(_find_common_type(src), common)
231224

232225
def test_pandas_dtypes(self):
226+
# TODO: not implemented yet
233227
with self.assertRaises(TypeError):
234228
self.assertEqual(_find_common_type([CategoricalDtype()]),
235229
CategoricalDtype)

0 commit comments

Comments
 (0)