Skip to content

TST: Parametrize dtypes tests - test_common.py and test_concat.py #20340

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 2 commits into from
Mar 15, 2018
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
58 changes: 30 additions & 28 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,46 @@ class TestPandasDtype(object):

# Passing invalid dtype, both as a string or object, must raise TypeError
# Per issue GH15520
def test_invalid_dtype_error(self):
msg = 'not understood'
invalid_list = [pd.Timestamp, 'pd.Timestamp', list]
for dtype in invalid_list:
with tm.assert_raises_regex(TypeError, msg):
com.pandas_dtype(dtype)

valid_list = [object, 'float64', np.object_, np.dtype('object'), 'O',
np.float64, float, np.dtype('float64')]
for dtype in valid_list:
com.pandas_dtype(dtype)

def test_numpy_dtype(self):
for dtype in ['M8[ns]', 'm8[ns]', 'object', 'float64', 'int64']:
assert com.pandas_dtype(dtype) == np.dtype(dtype)
@pytest.mark.parametrize('box', [pd.Timestamp, 'pd.Timestamp', list])
def test_invalid_dtype_error(self, box):
with tm.assert_raises_regex(TypeError, 'not understood'):
com.pandas_dtype(box)

@pytest.mark.parametrize('dtype', [
object, 'float64', np.object_, np.dtype('object'), 'O',
np.float64, float, np.dtype('float64')])
def test_pandas_dtype_valid(self, dtype):
assert com.pandas_dtype(dtype) == dtype
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this test is actually necessary? Split this off from the test_invalid_dtype_error, as it appears to be testing valid instead of invalid dtypes. Previously this didn't have any type of assert statement; it just called pandas_dtype and the test passed if no error was raised.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this is ok, this is just testing that pandas_dtype doesn't raise on numpy dtypes


@pytest.mark.parametrize('dtype', [
'M8[ns]', 'm8[ns]', 'object', 'float64', 'int64'])
def test_numpy_dtype(self, dtype):
assert com.pandas_dtype(dtype) == np.dtype(dtype)

def test_numpy_string_dtype(self):
# do not parse freq-like string as period dtype
assert com.pandas_dtype('U') == np.dtype('U')
assert com.pandas_dtype('S') == np.dtype('S')

def test_datetimetz_dtype(self):
for dtype in ['datetime64[ns, US/Eastern]',
'datetime64[ns, Asia/Tokyo]',
'datetime64[ns, UTC]']:
assert com.pandas_dtype(dtype) is DatetimeTZDtype(dtype)
assert com.pandas_dtype(dtype) == DatetimeTZDtype(dtype)
assert com.pandas_dtype(dtype) == dtype
@pytest.mark.parametrize('dtype', [
'datetime64[ns, US/Eastern]',
'datetime64[ns, Asia/Tokyo]',
'datetime64[ns, UTC]'])
def test_datetimetz_dtype(self, dtype):
assert com.pandas_dtype(dtype) is DatetimeTZDtype(dtype)
assert com.pandas_dtype(dtype) == DatetimeTZDtype(dtype)
assert com.pandas_dtype(dtype) == dtype

def test_categorical_dtype(self):
assert com.pandas_dtype('category') == CategoricalDtype()

def test_period_dtype(self):
for dtype in ['period[D]', 'period[3M]', 'period[U]',
'Period[D]', 'Period[3M]', 'Period[U]']:
assert com.pandas_dtype(dtype) is PeriodDtype(dtype)
assert com.pandas_dtype(dtype) == PeriodDtype(dtype)
assert com.pandas_dtype(dtype) == dtype
@pytest.mark.parametrize('dtype', [
'period[D]', 'period[3M]', 'period[U]',
'Period[D]', 'Period[3M]', 'Period[U]'])
def test_period_dtype(self, dtype):
assert com.pandas_dtype(dtype) is PeriodDtype(dtype)
assert com.pandas_dtype(dtype) == PeriodDtype(dtype)
assert com.pandas_dtype(dtype) == dtype


dtypes = dict(datetime_tz=com.pandas_dtype('datetime64[ns, US/Eastern]'),
Expand Down
124 changes: 50 additions & 74 deletions pandas/tests/dtypes/test_concat.py
Original file line number Diff line number Diff line change
@@ -1,77 +1,53 @@
# -*- coding: utf-8 -*-

import pandas as pd
import pytest
import pandas.core.dtypes.concat as _concat


class TestConcatCompat(object):

def check_concat(self, to_concat, exp):
for klass in [pd.Index, pd.Series]:
to_concat_klass = [klass(c) for c in to_concat]
res = _concat.get_dtype_kinds(to_concat_klass)
assert res == set(exp)

def test_get_dtype_kinds(self):
to_concat = [['a'], [1, 2]]
self.check_concat(to_concat, ['i', 'object'])

to_concat = [[3, 4], [1, 2]]
self.check_concat(to_concat, ['i'])

to_concat = [[3, 4], [1, 2.1]]
self.check_concat(to_concat, ['i', 'f'])

def test_get_dtype_kinds_datetimelike(self):
to_concat = [pd.DatetimeIndex(['2011-01-01']),
pd.DatetimeIndex(['2011-01-02'])]
self.check_concat(to_concat, ['datetime'])

to_concat = [pd.TimedeltaIndex(['1 days']),
pd.TimedeltaIndex(['2 days'])]
self.check_concat(to_concat, ['timedelta'])

def test_get_dtype_kinds_datetimelike_object(self):
to_concat = [pd.DatetimeIndex(['2011-01-01']),
pd.DatetimeIndex(['2011-01-02'], tz='US/Eastern')]
self.check_concat(to_concat,
['datetime', 'datetime64[ns, US/Eastern]'])

to_concat = [pd.DatetimeIndex(['2011-01-01'], tz='Asia/Tokyo'),
pd.DatetimeIndex(['2011-01-02'], tz='US/Eastern')]
self.check_concat(to_concat,
['datetime64[ns, Asia/Tokyo]',
'datetime64[ns, US/Eastern]'])

# timedelta has single type
to_concat = [pd.TimedeltaIndex(['1 days']),
pd.TimedeltaIndex(['2 hours'])]
self.check_concat(to_concat, ['timedelta'])

to_concat = [pd.DatetimeIndex(['2011-01-01'], tz='Asia/Tokyo'),
pd.TimedeltaIndex(['1 days'])]
self.check_concat(to_concat,
['datetime64[ns, Asia/Tokyo]', 'timedelta'])

def test_get_dtype_kinds_period(self):
# because we don't have Period dtype (yet),
# Series results in object dtype
to_concat = [pd.PeriodIndex(['2011-01'], freq='M'),
pd.PeriodIndex(['2011-01'], freq='M')]
res = _concat.get_dtype_kinds(to_concat)
assert res == set(['period[M]'])

to_concat = [pd.Series([pd.Period('2011-01', freq='M')]),
pd.Series([pd.Period('2011-02', freq='M')])]
res = _concat.get_dtype_kinds(to_concat)
assert res == set(['object'])

to_concat = [pd.PeriodIndex(['2011-01'], freq='M'),
pd.PeriodIndex(['2011-01'], freq='D')]
res = _concat.get_dtype_kinds(to_concat)
assert res == set(['period[M]', 'period[D]'])

to_concat = [pd.Series([pd.Period('2011-01', freq='M')]),
pd.Series([pd.Period('2011-02', freq='D')])]
res = _concat.get_dtype_kinds(to_concat)
assert res == set(['object'])
from pandas import (
Index, DatetimeIndex, PeriodIndex, TimedeltaIndex, Series, Period)


@pytest.mark.parametrize('to_concat, expected', [
# int/float/str
([['a'], [1, 2]], ['i', 'object']),
([[3, 4], [1, 2]], ['i']),
([[3, 4], [1, 2.1]], ['i', 'f']),

# datetimelike
([DatetimeIndex(['2011-01-01']), DatetimeIndex(['2011-01-02'])],
['datetime']),
([TimedeltaIndex(['1 days']), TimedeltaIndex(['2 days'])],
['timedelta']),

# datetimelike object
([DatetimeIndex(['2011-01-01']),
DatetimeIndex(['2011-01-02'], tz='US/Eastern')],
['datetime', 'datetime64[ns, US/Eastern]']),
([DatetimeIndex(['2011-01-01'], tz='Asia/Tokyo'),
DatetimeIndex(['2011-01-02'], tz='US/Eastern')],
['datetime64[ns, Asia/Tokyo]', 'datetime64[ns, US/Eastern]']),
([TimedeltaIndex(['1 days']), TimedeltaIndex(['2 hours'])],
['timedelta']),
([DatetimeIndex(['2011-01-01'], tz='Asia/Tokyo'),
TimedeltaIndex(['1 days'])],
['datetime64[ns, Asia/Tokyo]', 'timedelta'])])
@pytest.mark.parametrize('klass', [Index, Series])
def test_get_dtype_kinds(klass, to_concat, expected):
to_concat_klass = [klass(c) for c in to_concat]
result = _concat.get_dtype_kinds(to_concat_klass)
assert result == set(expected)


@pytest.mark.parametrize('to_concat, expected', [
# because we don't have Period dtype (yet),
# Series results in object dtype
([PeriodIndex(['2011-01'], freq='M'),
PeriodIndex(['2011-01'], freq='M')], ['period[M]']),
([Series([Period('2011-01', freq='M')]),
Series([Period('2011-02', freq='M')])], ['object']),
([PeriodIndex(['2011-01'], freq='M'),
PeriodIndex(['2011-01'], freq='D')], ['period[M]', 'period[D]']),
([Series([Period('2011-01', freq='M')]),
Series([Period('2011-02', freq='D')])], ['object'])])
def test_get_dtype_kinds_period(to_concat, expected):
result = _concat.get_dtype_kinds(to_concat)
assert result == set(expected)