Skip to content

Commit 78677ef

Browse files
committed
TST: Parameterize more tests
* frame/test_dtypes.py * series/indexing/test_boolean.py * reshape/merge/test_merge_asof.py
1 parent 9d94a95 commit 78677ef

File tree

4 files changed

+207
-177
lines changed

4 files changed

+207
-177
lines changed

pandas/conftest.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,10 @@ def string_dtype(request):
259259
return request.param
260260

261261

262-
@pytest.fixture(params=[float, "float32", "float64"])
262+
FLOAT_DTYPES = [float, "float32", "float64"]
263+
264+
265+
@pytest.fixture(params=FLOAT_DTYPES)
263266
def float_dtype(request):
264267
"""
265268
Parameterized fixture for float dtypes.
@@ -286,6 +289,7 @@ def complex_dtype(request):
286289
UNSIGNED_INT_DTYPES = ["uint8", "uint16", "uint32", "uint64"]
287290
SIGNED_INT_DTYPES = [int, "int8", "int16", "int32", "int64"]
288291
ALL_INT_DTYPES = UNSIGNED_INT_DTYPES + SIGNED_INT_DTYPES
292+
ALL_REAL_DTYPES = FLOAT_DTYPES + ALL_INT_DTYPES
289293

290294

291295
@pytest.fixture(params=SIGNED_INT_DTYPES)
@@ -321,6 +325,26 @@ def any_int_dtype(request):
321325
"""
322326
Parameterized fixture for any integer dtypes.
323327
328+
* int8
329+
* uint8
330+
* int16
331+
* uint16
332+
* int32
333+
* uint32
334+
* int64
335+
* uint64
336+
* float32
337+
* float64
338+
"""
339+
340+
return request.param
341+
342+
343+
@pytest.fixture(params=ALL_REAL_DTYPES)
344+
def any_real_dtype(request):
345+
"""
346+
Parameterized fixture for any (purely) real numeric dtypes.
347+
324348
* int8
325349
* uint8
326350
* int16

pandas/tests/frame/test_dtypes.py

+72-72
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import numpy as np
1010
from pandas import (DataFrame, Series, date_range, Timedelta, Timestamp,
1111
Categorical, compat, concat, option_context)
12-
from pandas.compat import u
12+
from pandas.compat import u, PY2
1313
from pandas import _np_version_under1p14
1414

1515
from pandas.core.dtypes.dtypes import DatetimeTZDtype, CategoricalDtype
@@ -21,6 +21,11 @@
2121
import pandas as pd
2222

2323

24+
@pytest.fixture(params=[str, compat.text_type])
25+
def text_dtype(request):
26+
return request.param
27+
28+
2429
class TestDataFrameDataTypes(TestData):
2530

2631
def test_concat_empty_dataframe_dtypes(self):
@@ -351,27 +356,24 @@ def test_select_dtypes_datetime_with_tz(self):
351356
expected = df3.reindex(columns=[])
352357
assert_frame_equal(result, expected)
353358

354-
def test_select_dtypes_str_raises(self):
355-
df = DataFrame({'a': list('abc'),
356-
'g': list(u('abc')),
357-
'b': list(range(1, 4)),
358-
'c': np.arange(3, 6).astype('u1'),
359-
'd': np.arange(4.0, 7.0, dtype='float64'),
360-
'e': [True, False, True],
361-
'f': pd.date_range('now', periods=3).values})
362-
string_dtypes = set((str, 'str', np.string_, 'S1',
363-
'unicode', np.unicode_, 'U1'))
364-
try:
365-
string_dtypes.add(unicode)
366-
except NameError:
367-
pass
368-
for dt in string_dtypes:
369-
with tm.assert_raises_regex(TypeError,
370-
'string dtypes are not allowed'):
371-
df.select_dtypes(include=[dt])
372-
with tm.assert_raises_regex(TypeError,
373-
'string dtypes are not allowed'):
374-
df.select_dtypes(exclude=[dt])
359+
@pytest.mark.parametrize(
360+
"dtype", {str, "str", np.string_, "S1",
361+
"unicode", np.unicode_, "U1"}
362+
.union({unicode} if PY2 else {}))
363+
@pytest.mark.parametrize("arg", ["include", "exclude"])
364+
def test_select_dtypes_str_raises(self, dtype, arg):
365+
df = DataFrame({"a": list("abc"),
366+
"g": list(u("abc")),
367+
"b": list(range(1, 4)),
368+
"c": np.arange(3, 6).astype("u1"),
369+
"d": np.arange(4.0, 7.0, dtype="float64"),
370+
"e": [True, False, True],
371+
"f": pd.date_range("now", periods=3).values})
372+
msg = "string dtypes are not allowed"
373+
kwargs = {arg: [dtype]}
374+
375+
with tm.assert_raises_regex(TypeError, msg):
376+
df.select_dtypes(**kwargs)
375377

376378
def test_select_dtypes_bad_arg_raises(self):
377379
df = DataFrame({'a': list('abc'),
@@ -502,61 +504,59 @@ def test_astype_with_view(self):
502504
tf = self.frame.astype(np.float64)
503505
casted = tf.astype(np.int64, copy=False) # noqa
504506

505-
def test_astype_cast_nan_inf_int(self):
506-
# GH14265, check nan and inf raise error when converting to int
507-
types = [np.int32, np.int64]
508-
values = [np.nan, np.inf]
509-
msg = 'Cannot convert non-finite values \\(NA or inf\\) to integer'
507+
@pytest.mark.parametrize("dtype", [np.int32, np.int64])
508+
@pytest.mark.parametrize("val", [np.nan, np.inf])
509+
def test_astype_cast_nan_inf_int(self, val, dtype):
510+
# see gh-14265
511+
#
512+
# Check NaN and inf --> raise error when converting to int.
513+
msg = "Cannot convert non-finite values \\(NA or inf\\) to integer"
514+
df = DataFrame([val])
510515

511-
for this_type in types:
512-
for this_val in values:
513-
df = DataFrame([this_val])
514-
with tm.assert_raises_regex(ValueError, msg):
515-
df.astype(this_type)
516+
with tm.assert_raises_regex(ValueError, msg):
517+
df.astype(dtype)
516518

517-
def test_astype_str(self):
518-
# GH9757
519-
a = Series(date_range('2010-01-04', periods=5))
520-
b = Series(date_range('3/6/2012 00:00', periods=5, tz='US/Eastern'))
521-
c = Series([Timedelta(x, unit='d') for x in range(5)])
519+
def test_astype_str(self, text_dtype):
520+
# see gh-9757
521+
a = Series(date_range("2010-01-04", periods=5))
522+
b = Series(date_range("3/6/2012 00:00", periods=5, tz="US/Eastern"))
523+
c = Series([Timedelta(x, unit="d") for x in range(5)])
522524
d = Series(range(5))
523525
e = Series([0.0, 0.2, 0.4, 0.6, 0.8])
524526

525-
df = DataFrame({'a': a, 'b': b, 'c': c, 'd': d, 'e': e})
526-
527-
# datetimelike
528-
# Test str and unicode on python 2.x and just str on python 3.x
529-
for tt in set([str, compat.text_type]):
530-
result = df.astype(tt)
531-
532-
expected = DataFrame({
533-
'a': list(map(tt, map(lambda x: Timestamp(x)._date_repr,
534-
a._values))),
535-
'b': list(map(tt, map(Timestamp, b._values))),
536-
'c': list(map(tt, map(lambda x: Timedelta(x)
537-
._repr_base(format='all'), c._values))),
538-
'd': list(map(tt, d._values)),
539-
'e': list(map(tt, e._values)),
540-
})
541-
542-
assert_frame_equal(result, expected)
543-
544-
# float/nan
545-
# 11302
546-
# consistency in astype(str)
547-
for tt in set([str, compat.text_type]):
548-
result = DataFrame([np.NaN]).astype(tt)
549-
expected = DataFrame(['nan'])
550-
assert_frame_equal(result, expected)
551-
552-
result = DataFrame([1.12345678901234567890]).astype(tt)
553-
if _np_version_under1p14:
554-
# < 1.14 truncates
555-
expected = DataFrame(['1.12345678901'])
556-
else:
557-
# >= 1.14 preserves the full repr
558-
expected = DataFrame(['1.1234567890123457'])
559-
assert_frame_equal(result, expected)
527+
df = DataFrame({"a": a, "b": b, "c": c, "d": d, "e": e})
528+
529+
# Datetime-like
530+
# Test str and unicode on Python 2.x and just str on Python 3.x
531+
result = df.astype(text_dtype)
532+
533+
expected = DataFrame({
534+
"a": list(map(text_dtype,
535+
map(lambda x: Timestamp(x)._date_repr, a._values))),
536+
"b": list(map(text_dtype, map(Timestamp, b._values))),
537+
"c": list(map(text_dtype,
538+
map(lambda x: Timedelta(x)._repr_base(format="all"),
539+
c._values))),
540+
"d": list(map(text_dtype, d._values)),
541+
"e": list(map(text_dtype, e._values)),
542+
})
543+
544+
assert_frame_equal(result, expected)
545+
546+
def test_astype_str_float(self, text_dtype):
547+
# see gh-11302
548+
result = DataFrame([np.NaN]).astype(text_dtype)
549+
expected = DataFrame(["nan"])
550+
551+
assert_frame_equal(result, expected)
552+
result = DataFrame([1.12345678901234567890]).astype(text_dtype)
553+
554+
# < 1.14 truncates
555+
# >= 1.14 preserves the full repr
556+
val = ("1.12345678901" if _np_version_under1p14
557+
else "1.1234567890123457")
558+
expected = DataFrame([val])
559+
assert_frame_equal(result, expected)
560560

561561
@pytest.mark.parametrize("dtype_class", [dict, Series])
562562
def test_astype_dict_like(self, dtype_class):

pandas/tests/reshape/merge/test_merge_asof.py

+58-71
Original file line numberDiff line numberDiff line change
@@ -892,77 +892,64 @@ def test_on_float(self):
892892

893893
assert_frame_equal(result, expected)
894894

895-
def test_on_specialized_type(self):
896-
# GH13936
897-
for dtype in [np.uint8, np.uint16, np.uint32, np.uint64,
898-
np.int8, np.int16, np.int32, np.int64,
899-
np.float16, np.float32, np.float64]:
900-
df1 = pd.DataFrame({
901-
'value': [5, 2, 25, 100, 78, 120, 79],
902-
'symbol': list("ABCDEFG")},
903-
columns=['symbol', 'value'])
904-
df1.value = dtype(df1.value)
905-
906-
df2 = pd.DataFrame({
907-
'value': [0, 80, 120, 125],
908-
'result': list('xyzw')},
909-
columns=['value', 'result'])
910-
df2.value = dtype(df2.value)
911-
912-
df1 = df1.sort_values('value').reset_index(drop=True)
913-
914-
if dtype == np.float16:
915-
with pytest.raises(MergeError):
916-
pd.merge_asof(df1, df2, on='value')
917-
continue
918-
919-
result = pd.merge_asof(df1, df2, on='value')
920-
921-
expected = pd.DataFrame(
922-
{'symbol': list("BACEGDF"),
923-
'value': [2, 5, 25, 78, 79, 100, 120],
924-
'result': list('xxxxxyz')
925-
}, columns=['symbol', 'value', 'result'])
926-
expected.value = dtype(expected.value)
927-
928-
assert_frame_equal(result, expected)
929-
930-
def test_on_specialized_type_by_int(self):
931-
# GH13936
932-
for dtype in [np.uint8, np.uint16, np.uint32, np.uint64,
933-
np.int8, np.int16, np.int32, np.int64,
934-
np.float16, np.float32, np.float64]:
935-
df1 = pd.DataFrame({
936-
'value': [5, 2, 25, 100, 78, 120, 79],
937-
'key': [1, 2, 3, 2, 3, 1, 2],
938-
'symbol': list("ABCDEFG")},
939-
columns=['symbol', 'key', 'value'])
940-
df1.value = dtype(df1.value)
941-
942-
df2 = pd.DataFrame({
943-
'value': [0, 80, 120, 125],
944-
'key': [1, 2, 2, 3],
945-
'result': list('xyzw')},
946-
columns=['value', 'key', 'result'])
947-
df2.value = dtype(df2.value)
948-
949-
df1 = df1.sort_values('value').reset_index(drop=True)
950-
951-
if dtype == np.float16:
952-
with pytest.raises(MergeError):
953-
pd.merge_asof(df1, df2, on='value', by='key')
954-
else:
955-
result = pd.merge_asof(df1, df2, on='value', by='key')
956-
957-
expected = pd.DataFrame({
958-
'symbol': list("BACEGDF"),
959-
'key': [2, 1, 3, 3, 2, 2, 1],
960-
'value': [2, 5, 25, 78, 79, 100, 120],
961-
'result': [np.nan, 'x', np.nan, np.nan, np.nan, 'y', 'x']},
962-
columns=['symbol', 'key', 'value', 'result'])
963-
expected.value = dtype(expected.value)
964-
965-
assert_frame_equal(result, expected)
895+
def test_on_specialized_type(self, any_real_dtype):
896+
# see gh-13936
897+
dtype = np.dtype(any_real_dtype).type
898+
899+
df1 = pd.DataFrame({
900+
"value": [5, 2, 25, 100, 78, 120, 79],
901+
"symbol": list("ABCDEFG")},
902+
columns=["symbol", "value"])
903+
df1.value = dtype(df1.value)
904+
905+
df2 = pd.DataFrame({
906+
"value": [0, 80, 120, 125],
907+
"result": list("xyzw")},
908+
columns=["value", "result"])
909+
df2.value = dtype(df2.value)
910+
911+
df1 = df1.sort_values("value").reset_index(drop=True)
912+
result = pd.merge_asof(df1, df2, on="value")
913+
914+
expected = pd.DataFrame(
915+
{"symbol": list("BACEGDF"),
916+
"value": [2, 5, 25, 78, 79, 100, 120],
917+
"result": list("xxxxxyz")
918+
}, columns=["symbol", "value", "result"])
919+
expected.value = dtype(expected.value)
920+
921+
assert_frame_equal(result, expected)
922+
923+
def test_on_specialized_type_by_int(self, any_real_dtype):
924+
# see gh-13936
925+
dtype = np.dtype(any_real_dtype).type
926+
927+
df1 = pd.DataFrame({
928+
"value": [5, 2, 25, 100, 78, 120, 79],
929+
"key": [1, 2, 3, 2, 3, 1, 2],
930+
"symbol": list("ABCDEFG")},
931+
columns=["symbol", "key", "value"])
932+
df1.value = dtype(df1.value)
933+
934+
df2 = pd.DataFrame({
935+
"value": [0, 80, 120, 125],
936+
"key": [1, 2, 2, 3],
937+
"result": list("xyzw")},
938+
columns=["value", "key", "result"])
939+
df2.value = dtype(df2.value)
940+
941+
df1 = df1.sort_values("value").reset_index(drop=True)
942+
result = pd.merge_asof(df1, df2, on="value", by="key")
943+
944+
expected = pd.DataFrame({
945+
"symbol": list("BACEGDF"),
946+
"key": [2, 1, 3, 3, 2, 2, 1],
947+
"value": [2, 5, 25, 78, 79, 100, 120],
948+
"result": [np.nan, "x", np.nan, np.nan, np.nan, "y", "x"]},
949+
columns=["symbol", "key", "value", "result"])
950+
expected.value = dtype(expected.value)
951+
952+
assert_frame_equal(result, expected)
966953

967954
def test_on_float_by_int(self):
968955
# type specialize both "by" and "on" parameters

0 commit comments

Comments
 (0)