|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from pandas import compat |
| 4 | +from pandas.util.decorators import cache_readonly |
| 5 | +import pandas.util.testing as tm |
| 6 | +import pandas as pd |
| 7 | + |
| 8 | +_seriesd = tm.getSeriesData() |
| 9 | +_tsd = tm.getTimeSeriesData() |
| 10 | + |
| 11 | +_frame = pd.DataFrame(_seriesd) |
| 12 | +_frame2 = pd.DataFrame(_seriesd, columns=['D', 'C', 'B', 'A']) |
| 13 | +_intframe = pd.DataFrame(dict((k, v.astype(int)) |
| 14 | + for k, v in compat.iteritems(_seriesd))) |
| 15 | + |
| 16 | +_tsframe = pd.DataFrame(_tsd) |
| 17 | + |
| 18 | +_mixed_frame = _frame.copy() |
| 19 | +_mixed_frame['foo'] = 'bar' |
| 20 | + |
| 21 | + |
| 22 | +class TestData(object): |
| 23 | + |
| 24 | + @cache_readonly |
| 25 | + def frame(self): |
| 26 | + return _frame.copy() |
| 27 | + |
| 28 | + @cache_readonly |
| 29 | + def frame2(self): |
| 30 | + return _frame2.copy() |
| 31 | + |
| 32 | + @cache_readonly |
| 33 | + def intframe(self): |
| 34 | + # force these all to int64 to avoid platform testing issues |
| 35 | + return pd.DataFrame(dict([(c, s) for c, s in |
| 36 | + compat.iteritems(_intframe)]), |
| 37 | + dtype=np.int64) |
| 38 | + |
| 39 | + @cache_readonly |
| 40 | + def tsframe(self): |
| 41 | + return _tsframe.copy() |
| 42 | + |
| 43 | + @cache_readonly |
| 44 | + def mixed_frame(self): |
| 45 | + return _mixed_frame.copy() |
| 46 | + |
| 47 | + @cache_readonly |
| 48 | + def mixed_float(self): |
| 49 | + return pd.DataFrame({'A': _frame['A'].copy().astype('float32'), |
| 50 | + 'B': _frame['B'].copy().astype('float32'), |
| 51 | + 'C': _frame['C'].copy().astype('float16'), |
| 52 | + 'D': _frame['D'].copy().astype('float64')}) |
| 53 | + |
| 54 | + @cache_readonly |
| 55 | + def mixed_float2(self): |
| 56 | + return pd.DataFrame({'A': _frame2['A'].copy().astype('float32'), |
| 57 | + 'B': _frame2['B'].copy().astype('float32'), |
| 58 | + 'C': _frame2['C'].copy().astype('float16'), |
| 59 | + 'D': _frame2['D'].copy().astype('float64')}) |
| 60 | + |
| 61 | + @cache_readonly |
| 62 | + def mixed_int(self): |
| 63 | + return pd.DataFrame({'A': _intframe['A'].copy().astype('int32'), |
| 64 | + 'B': np.ones(len(_intframe['B']), dtype='uint64'), |
| 65 | + 'C': _intframe['C'].copy().astype('uint8'), |
| 66 | + 'D': _intframe['D'].copy().astype('int64')}) |
| 67 | + |
| 68 | + @cache_readonly |
| 69 | + def all_mixed(self): |
| 70 | + return pd.DataFrame({'a': 1., 'b': 2, 'c': 'foo', |
| 71 | + 'float32': np.array([1.] * 10, dtype='float32'), |
| 72 | + 'int32': np.array([1] * 10, dtype='int32')}, |
| 73 | + index=np.arange(10)) |
| 74 | + |
| 75 | + @cache_readonly |
| 76 | + def tzframe(self): |
| 77 | + result = pd.DataFrame({'A': pd.date_range('20130101', periods=3), |
| 78 | + 'B': pd.date_range('20130101', periods=3, |
| 79 | + tz='US/Eastern'), |
| 80 | + 'C': pd.date_range('20130101', periods=3, |
| 81 | + tz='CET')}) |
| 82 | + result.iloc[1, 1] = pd.NaT |
| 83 | + result.iloc[1, 2] = pd.NaT |
| 84 | + return result |
| 85 | + |
| 86 | + @cache_readonly |
| 87 | + def empty(self): |
| 88 | + return pd.DataFrame({}) |
| 89 | + |
| 90 | + @cache_readonly |
| 91 | + def ts1(self): |
| 92 | + return tm.makeTimeSeries() |
| 93 | + |
| 94 | + @cache_readonly |
| 95 | + def ts2(self): |
| 96 | + return tm.makeTimeSeries()[5:] |
| 97 | + |
| 98 | + @cache_readonly |
| 99 | + def simple(self): |
| 100 | + arr = np.array([[1., 2., 3.], |
| 101 | + [4., 5., 6.], |
| 102 | + [7., 8., 9.]]) |
| 103 | + |
| 104 | + return pd.DataFrame(arr, columns=['one', 'two', 'three'], |
| 105 | + index=['a', 'b', 'c']) |
| 106 | + |
| 107 | +# self.ts3 = tm.makeTimeSeries()[-5:] |
| 108 | +# self.ts4 = tm.makeTimeSeries()[1:-1] |
| 109 | + |
| 110 | + |
| 111 | +def _check_mixed_float(df, dtype=None): |
| 112 | + # float16 are most likely to be upcasted to float32 |
| 113 | + dtypes = dict(A='float32', B='float32', C='float16', D='float64') |
| 114 | + if isinstance(dtype, compat.string_types): |
| 115 | + dtypes = dict([(k, dtype) for k, v in dtypes.items()]) |
| 116 | + elif isinstance(dtype, dict): |
| 117 | + dtypes.update(dtype) |
| 118 | + if dtypes.get('A'): |
| 119 | + assert(df.dtypes['A'] == dtypes['A']) |
| 120 | + if dtypes.get('B'): |
| 121 | + assert(df.dtypes['B'] == dtypes['B']) |
| 122 | + if dtypes.get('C'): |
| 123 | + assert(df.dtypes['C'] == dtypes['C']) |
| 124 | + if dtypes.get('D'): |
| 125 | + assert(df.dtypes['D'] == dtypes['D']) |
| 126 | + |
| 127 | + |
| 128 | +def _check_mixed_int(df, dtype=None): |
| 129 | + dtypes = dict(A='int32', B='uint64', C='uint8', D='int64') |
| 130 | + if isinstance(dtype, compat.string_types): |
| 131 | + dtypes = dict([(k, dtype) for k, v in dtypes.items()]) |
| 132 | + elif isinstance(dtype, dict): |
| 133 | + dtypes.update(dtype) |
| 134 | + if dtypes.get('A'): |
| 135 | + assert(df.dtypes['A'] == dtypes['A']) |
| 136 | + if dtypes.get('B'): |
| 137 | + assert(df.dtypes['B'] == dtypes['B']) |
| 138 | + if dtypes.get('C'): |
| 139 | + assert(df.dtypes['C'] == dtypes['C']) |
| 140 | + if dtypes.get('D'): |
| 141 | + assert(df.dtypes['D'] == dtypes['D']) |
0 commit comments