Skip to content

TST: Add period and other dtype related tests #12748

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

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 14 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,20 @@ def create_data(constructor):
assert_frame_equal(result_timedelta, expected)
assert_frame_equal(result_Timedelta, expected)

def test_constructor_period(self):
# PeriodIndex
a = pd.PeriodIndex(['2012-01', 'NaT', '2012-04'], freq='M')
b = pd.PeriodIndex(['2012-02-01', '2012-03-01', 'NaT'], freq='D')
df = pd.DataFrame({'a': a, 'b': b})
self.assertEqual(df['a'].dtype, 'object')
self.assertEqual(df['b'].dtype, 'object')

# list of periods
df = pd.DataFrame({'a': a.asobject.tolist(),
'b': b.asobject.tolist()})
self.assertEqual(df['a'].dtype, 'object')
self.assertEqual(df['b'].dtype, 'object')

def test_nested_dict_frame_constructor(self):
rng = pd.period_range('1/1/2000', periods=5)
df = DataFrame(randn(10, 5), columns=rng)
Expand Down
44 changes: 44 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,50 @@ def test_construction_list_mixed_tuples(self):
self.assertIsInstance(idx2, Index) and self.assertNotInstance(
idx2, MultiIndex)

def test_constructor_from_index_datetimetz(self):
idx = pd.date_range('2015-01-01 10:00', freq='D', periods=3,
tz='US/Eastern')
result = pd.Index(idx)
tm.assert_index_equal(result, idx)
self.assertEqual(result.tz, idx.tz)

result = pd.Index(idx.asobject)
tm.assert_index_equal(result, idx)
self.assertEqual(result.tz, idx.tz)

def test_constructor_from_index_timedelta(self):
idx = pd.timedelta_range('1 days', freq='D', periods=3)
result = pd.Index(idx)
tm.assert_index_equal(result, idx)

result = pd.Index(idx.asobject)
tm.assert_index_equal(result, idx)

def test_constructor_from_index_period(self):
idx = pd.period_range('2015-01-01', freq='D', periods=3)
result = pd.Index(idx)
tm.assert_index_equal(result, idx)

result = pd.Index(idx.asobject)
tm.assert_index_equal(result, idx)

def test_constructor_from_series_datetimetz(self):
idx = pd.date_range('2015-01-01 10:00', freq='D', periods=3,
tz='US/Eastern')
result = pd.Index(pd.Series(idx))
tm.assert_index_equal(result, idx)
self.assertEqual(result.tz, idx.tz)

def test_constructor_from_series_timedelta(self):
idx = pd.timedelta_range('1 days', freq='D', periods=3)
result = pd.Index(pd.Series(idx))
tm.assert_index_equal(result, idx)

def test_constructor_from_series_period(self):
idx = pd.period_range('2015-01-01', freq='D', periods=3)
result = pd.Index(pd.Series(idx))
tm.assert_index_equal(result, idx)

def test_constructor_from_series(self):

expected = DatetimeIndex([Timestamp('20110101'), Timestamp('20120101'),
Expand Down
41 changes: 41 additions & 0 deletions pandas/tests/indexes/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,47 @@ def test_from_arrays(self):
])))
self.assertTrue(result.levels[1].equals(Index(['a', 'b'])))

def test_from_arrays_index_series_datetimetz(self):
idx1 = pd.date_range('2015-01-01 10:00', freq='D', periods=3,
tz='US/Eastern')
idx2 = pd.date_range('2015-01-01 10:00', freq='H', periods=3,
tz='Asia/Tokyo')
result = pd.MultiIndex.from_arrays([idx1, idx2])
tm.assert_index_equal(result.get_level_values(0), idx1)
tm.assert_index_equal(result.get_level_values(1), idx2)

result2 = pd.MultiIndex.from_arrays([pd.Series(idx1), pd.Series(idx2)])
tm.assert_index_equal(result2.get_level_values(0), idx1)
tm.assert_index_equal(result2.get_level_values(1), idx2)

tm.assert_index_equal(result, result2)

def test_from_arrays_index_series_timedelta(self):
idx1 = pd.timedelta_range('1 days', freq='D', periods=3)
idx2 = pd.timedelta_range('2 hours', freq='H', periods=3)
result = pd.MultiIndex.from_arrays([idx1, idx2])
tm.assert_index_equal(result.get_level_values(0), idx1)
tm.assert_index_equal(result.get_level_values(1), idx2)

result2 = pd.MultiIndex.from_arrays([pd.Series(idx1), pd.Series(idx2)])
tm.assert_index_equal(result2.get_level_values(0), idx1)
tm.assert_index_equal(result2.get_level_values(1), idx2)

tm.assert_index_equal(result, result2)

def test_from_arrays_index_series_period(self):
idx1 = pd.period_range('2011-01-01', freq='D', periods=3)
idx2 = pd.period_range('2015-01-01', freq='H', periods=3)
result = pd.MultiIndex.from_arrays([idx1, idx2])
tm.assert_index_equal(result.get_level_values(0), idx1)
tm.assert_index_equal(result.get_level_values(1), idx2)

result2 = pd.MultiIndex.from_arrays([pd.Series(idx1), pd.Series(idx2)])
tm.assert_index_equal(result2.get_level_values(0), idx1)
tm.assert_index_equal(result2.get_level_values(1), idx2)

tm.assert_index_equal(result, result2)

def test_from_product(self):

first = ['foo', 'bar', 'buz']
Expand Down
2 changes: 2 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,8 @@ def test_constructor_periodindex(self):
expected = Series(pi.asobject)
assert_series_equal(s, expected)

self.assertEqual(s.dtype, 'object')

def test_constructor_dict(self):
d = {'a': 0., 'b': 1., 'c': 2.}
result = Series(d, index=['b', 'c', 'd', 'a'])
Expand Down
25 changes: 25 additions & 0 deletions pandas/tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,31 @@ def test_constructor_with_datetimelike(self):
result = repr(c)
self.assertTrue('NaT' in result)

def test_constructor_from_index_series_datetimetz(self):
idx = pd.date_range('2015-01-01 10:00', freq='D', periods=3,
tz='US/Eastern')
result = pd.Categorical.from_array(idx)
tm.assert_index_equal(result.categories, idx)

result = pd.Categorical.from_array(pd.Series(idx))
tm.assert_index_equal(result.categories, idx)

def test_constructor_from_index_series_timedelta(self):
idx = pd.timedelta_range('1 days', freq='D', periods=3)
result = pd.Categorical.from_array(idx)
tm.assert_index_equal(result.categories, idx)

result = pd.Categorical.from_array(pd.Series(idx))
tm.assert_index_equal(result.categories, idx)

def test_constructor_from_index_series_period(self):
idx = pd.period_range('2015-01-01', freq='D', periods=3)
result = pd.Categorical.from_array(idx)
tm.assert_index_equal(result.categories, idx)

result = pd.Categorical.from_array(pd.Series(idx))
tm.assert_index_equal(result.categories, idx)

def test_from_codes(self):

# too few categories
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import nose
import numpy as np
from pandas import Series, Categorical, date_range
import pandas.core.common as com
from pandas.core.common import (CategoricalDtype, is_categorical_dtype,
is_categorical, DatetimeTZDtype,
is_datetime64tz_dtype, is_datetimetz,
Expand Down Expand Up @@ -97,6 +98,12 @@ def test_subclass(self):
self.assertTrue(issubclass(type(a), type(a)))
self.assertTrue(issubclass(type(a), type(b)))

def test_coerce_to_dtype(self):
self.assertEqual(com._coerce_to_dtype('datetime64[ns, US/Eastern]'),
DatetimeTZDtype('ns', 'US/Eastern'))
self.assertEqual(com._coerce_to_dtype('datetime64[ns, Asia/Tokyo]'),
DatetimeTZDtype('ns', 'Asia/Tokyo'))

def test_compat(self):
self.assertFalse(is_datetime64_ns_dtype(self.dtype))
self.assertFalse(is_datetime64_ns_dtype('datetime64[ns, US/Eastern]'))
Expand Down
9 changes: 9 additions & 0 deletions pandas/tools/tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,8 @@ def test_merge_on_datetime64tz(self):
'key': [1., 2, 3]})
result = pd.merge(left, right, on='key', how='outer')
assert_frame_equal(result, expected)
self.assertEqual(result['value_x'].dtype, 'datetime64[ns, US/Eastern]')
self.assertEqual(result['value_y'].dtype, 'datetime64[ns, US/Eastern]')

def test_merge_on_periods(self):
left = pd.DataFrame({'key': pd.period_range('20151010', periods=2,
Expand Down Expand Up @@ -1095,6 +1097,8 @@ def test_merge_on_periods(self):
'key': [1., 2, 3]})
result = pd.merge(left, right, on='key', how='outer')
assert_frame_equal(result, expected)
self.assertEqual(result['value_x'].dtype, 'object')
self.assertEqual(result['value_y'].dtype, 'object')

def test_concat_NaT_series(self):
# GH 11693
Expand Down Expand Up @@ -1216,32 +1220,37 @@ def test_concat_period_series(self):
expected = Series([x[0], x[1], y[0], y[1]], dtype='object')
result = concat([x, y], ignore_index=True)
tm.assert_series_equal(result, expected)
self.assertEqual(result.dtype, 'object')

# different freq
x = Series(pd.PeriodIndex(['2015-11-01', '2015-12-01'], freq='D'))
y = Series(pd.PeriodIndex(['2015-10-01', '2016-01-01'], freq='M'))
expected = Series([x[0], x[1], y[0], y[1]], dtype='object')
result = concat([x, y], ignore_index=True)
tm.assert_series_equal(result, expected)
self.assertEqual(result.dtype, 'object')

x = Series(pd.PeriodIndex(['2015-11-01', '2015-12-01'], freq='D'))
y = Series(pd.PeriodIndex(['2015-11-01', '2015-12-01'], freq='M'))
expected = Series([x[0], x[1], y[0], y[1]], dtype='object')
result = concat([x, y], ignore_index=True)
tm.assert_series_equal(result, expected)
self.assertEqual(result.dtype, 'object')

# non-period
x = Series(pd.PeriodIndex(['2015-11-01', '2015-12-01'], freq='D'))
y = Series(pd.DatetimeIndex(['2015-11-01', '2015-12-01']))
expected = Series([x[0], x[1], y[0], y[1]], dtype='object')
result = concat([x, y], ignore_index=True)
tm.assert_series_equal(result, expected)
self.assertEqual(result.dtype, 'object')

x = Series(pd.PeriodIndex(['2015-11-01', '2015-12-01'], freq='D'))
y = Series(['A', 'B'])
expected = Series([x[0], x[1], y[0], y[1]], dtype='object')
result = concat([x, y], ignore_index=True)
tm.assert_series_equal(result, expected)
self.assertEqual(result.dtype, 'object')

def test_indicator(self):
# PR #10054. xref #7412 and closes #8790.
Expand Down
19 changes: 19 additions & 0 deletions pandas/tseries/tests/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -3833,11 +3833,30 @@ def test_auto_conversion(self):
series = Series(list(period_range('2000-01-01', periods=10, freq='D')))
self.assertEqual(series.dtype, 'object')

series = pd.Series([pd.Period('2011-01-01', freq='D'),
pd.Period('2011-02-01', freq='D')])
self.assertEqual(series.dtype, 'object')

def test_getitem(self):
self.assertEqual(self.series[1], pd.Period('2000-01-02', freq='D'))

result = self.series[[2, 4]]
exp = pd.Series([pd.Period('2000-01-03', freq='D'),
pd.Period('2000-01-05', freq='D')],
index=[2, 4])
self.assert_series_equal(result, exp)
self.assertEqual(result.dtype, 'object')

def test_constructor_cant_cast_period(self):
with tm.assertRaises(TypeError):
Series(period_range('2000-01-01', periods=10, freq='D'),
dtype=float)

def test_constructor_cast_object(self):
s = Series(period_range('1/1/2000', periods=10), dtype=object)
exp = Series(period_range('1/1/2000', periods=10))
tm.assert_series_equal(s, exp)

def test_series_comparison_scalars(self):
val = pd.Period('2000-01-04', freq='D')
result = self.series > val
Expand Down
16 changes: 14 additions & 2 deletions pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -3881,8 +3881,17 @@ def test_auto_conversion(self):
self.assertEqual(series.dtype, 'M8[ns]')

def test_constructor_cant_cast_datetime64(self):
self.assertRaises(TypeError, Series,
date_range('1/1/2000', periods=10), dtype=float)
msg = "Cannot cast datetime64 to "
with tm.assertRaisesRegexp(TypeError, msg):
Series(date_range('1/1/2000', periods=10), dtype=float)

with tm.assertRaisesRegexp(TypeError, msg):
Series(date_range('1/1/2000', periods=10), dtype=int)

def test_constructor_cast_object(self):
s = Series(date_range('1/1/2000', periods=10), dtype=object)
exp = Series(date_range('1/1/2000', periods=10))
tm.assert_series_equal(s, exp)

def test_series_comparison_scalars(self):
val = datetime(2000, 1, 4)
Expand Down Expand Up @@ -3941,6 +3950,9 @@ def test_intercept_astype_object(self):

df = DataFrame({'a': self.series,
'b': np.random.randn(len(self.series))})
exp_dtypes = pd.Series([np.dtype('datetime64[ns]'),
np.dtype('float64')], index=['a', 'b'])
tm.assert_series_equal(df.dtypes, exp_dtypes)

result = df.values.squeeze()
self.assertTrue((result[:, 0] == expected.values).all())
Expand Down