|
13 | 13 | is_object_dtype, is_datetimetz,
|
14 | 14 | needs_i8_conversion)
|
15 | 15 | import pandas.util.testing as tm
|
16 |
| -from pandas import (Series, Index, DatetimeIndex, TimedeltaIndex, PeriodIndex, |
17 |
| - Timedelta, IntervalIndex, Interval) |
18 |
| -from pandas.compat import StringIO, PYPY |
| 16 | +from pandas import (Series, Index, DatetimeIndex, TimedeltaIndex, |
| 17 | + PeriodIndex, Timedelta, IntervalIndex, Interval, |
| 18 | + CategoricalIndex, Timestamp) |
| 19 | +from pandas.compat import StringIO, PYPY, long |
19 | 20 | from pandas.compat.numpy import np_array_datetime64_compat
|
20 | 21 | from pandas.core.base import PandasDelegate, NoNewAttributesMixin
|
21 | 22 | from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin
|
@@ -433,7 +434,7 @@ def test_value_counts_unique_nunique(self):
|
433 | 434 | # datetimetz Series returns array of Timestamp
|
434 | 435 | assert result[0] == orig[0]
|
435 | 436 | for r in result:
|
436 |
| - assert isinstance(r, pd.Timestamp) |
| 437 | + assert isinstance(r, Timestamp) |
437 | 438 | tm.assert_numpy_array_equal(result,
|
438 | 439 | orig._values.asobject.values)
|
439 | 440 | else:
|
@@ -1031,3 +1032,83 @@ def f():
|
1031 | 1032 |
|
1032 | 1033 | pytest.raises(AttributeError, f)
|
1033 | 1034 | assert not hasattr(t, "b")
|
| 1035 | + |
| 1036 | + |
| 1037 | +class TestToIterable(object): |
| 1038 | + # test that we convert an iterable to python types |
| 1039 | + |
| 1040 | + @pytest.mark.parametrize( |
| 1041 | + 'dtype, rdtype', |
| 1042 | + [ |
| 1043 | + ('int8', (int, long)), |
| 1044 | + ('int16', (int, long)), |
| 1045 | + ('int32', (int, long)), |
| 1046 | + ('int64', (int, long)), |
| 1047 | + ('uint8', int), |
| 1048 | + ('uint16', int), |
| 1049 | + ('uint32', long), |
| 1050 | + ('uint64', long), |
| 1051 | + ('float16', float), |
| 1052 | + ('float32', float), |
| 1053 | + ('float64', float), |
| 1054 | + ('datetime64[ns]', Timestamp), |
| 1055 | + ('datetime64[ns, US/Eastern]', Timestamp), |
| 1056 | + ('timedelta64[ns]', Timedelta), |
| 1057 | + ('object', object), |
| 1058 | + ('category', object)]) |
| 1059 | + @pytest.mark.parametrize( |
| 1060 | + 'method', |
| 1061 | + [ |
| 1062 | + lambda x: x.tolist(), |
| 1063 | + lambda x: list(x), |
| 1064 | + lambda x: list(x.__iter__()), |
| 1065 | + ], ids=['tolist', 'list', 'iter']) |
| 1066 | + @pytest.mark.parametrize('typ', [Series, Index]) |
| 1067 | + def test_iterable(self, typ, method, dtype, rdtype): |
| 1068 | + # gh-10904 |
| 1069 | + # gh-13258 |
| 1070 | + # coerce iteration to underlying python / pandas types |
| 1071 | + s = typ([1], dtype=dtype) |
| 1072 | + result = method(s)[0] |
| 1073 | + assert isinstance(result, rdtype) |
| 1074 | + |
| 1075 | + @pytest.mark.parametrize( |
| 1076 | + 'dtype, rdtype', |
| 1077 | + [ |
| 1078 | + ('int8', int), |
| 1079 | + ('int16', int), |
| 1080 | + ('int32', int), |
| 1081 | + ('int64', int), |
| 1082 | + ('uint8', int), |
| 1083 | + ('uint16', int), |
| 1084 | + ('uint32', int), |
| 1085 | + ('uint64', int), |
| 1086 | + ('float16', float), |
| 1087 | + ('float32', float), |
| 1088 | + ('float64', float), |
| 1089 | + ('datetime64[ns]', Timestamp), |
| 1090 | + ('datetime64[ns, US/Eastern]', Timestamp), |
| 1091 | + ('timedelta64[ns]', Timedelta), |
| 1092 | + ('object', int), |
| 1093 | + ('category', int)]) |
| 1094 | + @pytest.mark.parametrize('typ', [Series, Index]) |
| 1095 | + def test_iterable_map(self, typ, dtype, rdtype): |
| 1096 | + # gh-13236 |
| 1097 | + # coerce iteration to underlying python / pandas types |
| 1098 | + s = typ([1], dtype=dtype) |
| 1099 | + result = s.map(type)[0] |
| 1100 | + assert result is rdtype |
| 1101 | + |
| 1102 | + @pytest.mark.parametrize( |
| 1103 | + 'method', |
| 1104 | + [ |
| 1105 | + lambda x: x.tolist(), |
| 1106 | + lambda x: list(x), |
| 1107 | + lambda x: list(x.__iter__()), |
| 1108 | + ], ids=['tolist', 'list', 'iter']) |
| 1109 | + def test_categorial_datetimelike(self, method): |
| 1110 | + i = CategoricalIndex([Timestamp('1999-12-31'), |
| 1111 | + Timestamp('2000-12-31')]) |
| 1112 | + |
| 1113 | + result = method(i)[0] |
| 1114 | + assert isinstance(result, Timestamp) |
0 commit comments