test_numpy.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import numpy as np
  2. import pytest
  3. import pandas as pd
  4. from pandas import compat
  5. from pandas.core.arrays.numpy_ import PandasArray, PandasDtype
  6. import pandas.util.testing as tm
  7. from .. import base
  8. @pytest.fixture
  9. def dtype():
  10. return PandasDtype(np.dtype('float'))
  11. @pytest.fixture
  12. def data(allow_in_pandas, dtype):
  13. return PandasArray(np.arange(1, 101, dtype=dtype._dtype))
  14. @pytest.fixture
  15. def data_missing(allow_in_pandas):
  16. return PandasArray(np.array([np.nan, 1.0]))
  17. @pytest.fixture
  18. def data_for_sorting(allow_in_pandas):
  19. """Length-3 array with a known sort order.
  20. This should be three items [B, C, A] with
  21. A < B < C
  22. """
  23. return PandasArray(
  24. np.array([1, 2, 0])
  25. )
  26. @pytest.fixture
  27. def data_missing_for_sorting(allow_in_pandas):
  28. """Length-3 array with a known sort order.
  29. This should be three items [B, NA, A] with
  30. A < B and NA missing.
  31. """
  32. return PandasArray(
  33. np.array([1, np.nan, 0])
  34. )
  35. @pytest.fixture
  36. def data_for_grouping(allow_in_pandas):
  37. """Data for factorization, grouping, and unique tests.
  38. Expected to be like [B, B, NA, NA, A, A, B, C]
  39. Where A < B < C and NA is missing
  40. """
  41. a, b, c = np.arange(3)
  42. return PandasArray(np.array(
  43. [b, b, np.nan, np.nan, a, a, b, c]
  44. ))
  45. class BaseNumPyTests(object):
  46. pass
  47. class TestCasting(BaseNumPyTests, base.BaseCastingTests):
  48. pass
  49. class TestConstructors(BaseNumPyTests, base.BaseConstructorsTests):
  50. @pytest.mark.skip(reason="We don't register our dtype")
  51. # We don't want to register. This test should probably be split in two.
  52. def test_from_dtype(self, data):
  53. pass
  54. class TestDtype(BaseNumPyTests, base.BaseDtypeTests):
  55. @pytest.mark.skip(reason="Incorrect expected.")
  56. # we unsurprisingly clash with a NumPy name.
  57. def test_check_dtype(self, data):
  58. pass
  59. class TestGetitem(BaseNumPyTests, base.BaseGetitemTests):
  60. pass
  61. class TestGroupby(BaseNumPyTests, base.BaseGroupbyTests):
  62. pass
  63. class TestInterface(BaseNumPyTests, base.BaseInterfaceTests):
  64. pass
  65. class TestMethods(BaseNumPyTests, base.BaseMethodsTests):
  66. @pytest.mark.skip(reason="TODO: remove?")
  67. def test_value_counts(self, all_data, dropna):
  68. pass
  69. @pytest.mark.skip(reason="Incorrect expected")
  70. # We have a bool dtype, so the result is an ExtensionArray
  71. # but expected is not
  72. def test_combine_le(self, data_repeated):
  73. super(TestMethods, self).test_combine_le(data_repeated)
  74. class TestArithmetics(BaseNumPyTests, base.BaseArithmeticOpsTests):
  75. divmod_exc = None
  76. series_scalar_exc = None
  77. frame_scalar_exc = None
  78. series_array_exc = None
  79. def test_divmod_series_array(self, data):
  80. s = pd.Series(data)
  81. self._check_divmod_op(s, divmod, data, exc=None)
  82. @pytest.mark.skip("We implement ops")
  83. def test_error(self, data, all_arithmetic_operators):
  84. pass
  85. def test_arith_series_with_scalar(self, data, all_arithmetic_operators):
  86. if (compat.PY2 and
  87. all_arithmetic_operators in {'__div__', '__rdiv__'}):
  88. raise pytest.skip(
  89. "Matching NumPy int / int -> float behavior."
  90. )
  91. super(TestArithmetics, self).test_arith_series_with_scalar(
  92. data, all_arithmetic_operators
  93. )
  94. def test_arith_series_with_array(self, data, all_arithmetic_operators):
  95. if (compat.PY2 and
  96. all_arithmetic_operators in {'__div__', '__rdiv__'}):
  97. raise pytest.skip(
  98. "Matching NumPy int / int -> float behavior."
  99. )
  100. super(TestArithmetics, self).test_arith_series_with_array(
  101. data, all_arithmetic_operators
  102. )
  103. class TestPrinting(BaseNumPyTests, base.BasePrintingTests):
  104. pass
  105. class TestNumericReduce(BaseNumPyTests, base.BaseNumericReduceTests):
  106. def check_reduce(self, s, op_name, skipna):
  107. result = getattr(s, op_name)(skipna=skipna)
  108. # avoid coercing int -> float. Just cast to the actual numpy type.
  109. expected = getattr(s.astype(s.dtype._dtype), op_name)(skipna=skipna)
  110. tm.assert_almost_equal(result, expected)
  111. class TestBooleanReduce(BaseNumPyTests, base.BaseBooleanReduceTests):
  112. pass
  113. class TestMising(BaseNumPyTests, base.BaseMissingTests):
  114. pass
  115. class TestReshaping(BaseNumPyTests, base.BaseReshapingTests):
  116. @pytest.mark.skip("Incorrect parent test")
  117. # not actually a mixed concat, since we concat int and int.
  118. def test_concat_mixed_dtypes(self, data):
  119. super(TestReshaping, self).test_concat_mixed_dtypes(data)
  120. class TestSetitem(BaseNumPyTests, base.BaseSetitemTests):
  121. pass
  122. class TestParsing(BaseNumPyTests, base.BaseParsingTests):
  123. pass