common.py 4.5 KB

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