test_complex.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. from warnings import catch_warnings
  2. import numpy as np
  3. import pytest
  4. import pandas as pd
  5. from pandas import (
  6. DataFrame,
  7. Series,
  8. )
  9. import pandas._testing as tm
  10. from pandas.tests.io.pytables.common import ensure_clean_store
  11. from pandas.io.pytables import read_hdf
  12. def test_complex_fixed(tmp_path, setup_path):
  13. df = DataFrame(
  14. np.random.rand(4, 5).astype(np.complex64),
  15. index=list("abcd"),
  16. columns=list("ABCDE"),
  17. )
  18. path = tmp_path / setup_path
  19. df.to_hdf(path, "df")
  20. reread = read_hdf(path, "df")
  21. tm.assert_frame_equal(df, reread)
  22. df = DataFrame(
  23. np.random.rand(4, 5).astype(np.complex128),
  24. index=list("abcd"),
  25. columns=list("ABCDE"),
  26. )
  27. path = tmp_path / setup_path
  28. df.to_hdf(path, "df")
  29. reread = read_hdf(path, "df")
  30. tm.assert_frame_equal(df, reread)
  31. def test_complex_table(tmp_path, setup_path):
  32. df = DataFrame(
  33. np.random.rand(4, 5).astype(np.complex64),
  34. index=list("abcd"),
  35. columns=list("ABCDE"),
  36. )
  37. path = tmp_path / setup_path
  38. df.to_hdf(path, "df", format="table")
  39. reread = read_hdf(path, "df")
  40. tm.assert_frame_equal(df, reread)
  41. df = DataFrame(
  42. np.random.rand(4, 5).astype(np.complex128),
  43. index=list("abcd"),
  44. columns=list("ABCDE"),
  45. )
  46. path = tmp_path / setup_path
  47. df.to_hdf(path, "df", format="table", mode="w")
  48. reread = read_hdf(path, "df")
  49. tm.assert_frame_equal(df, reread)
  50. def test_complex_mixed_fixed(tmp_path, setup_path):
  51. complex64 = np.array(
  52. [1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex64
  53. )
  54. complex128 = np.array(
  55. [1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex128
  56. )
  57. df = DataFrame(
  58. {
  59. "A": [1, 2, 3, 4],
  60. "B": ["a", "b", "c", "d"],
  61. "C": complex64,
  62. "D": complex128,
  63. "E": [1.0, 2.0, 3.0, 4.0],
  64. },
  65. index=list("abcd"),
  66. )
  67. path = tmp_path / setup_path
  68. df.to_hdf(path, "df")
  69. reread = read_hdf(path, "df")
  70. tm.assert_frame_equal(df, reread)
  71. def test_complex_mixed_table(tmp_path, setup_path):
  72. complex64 = np.array(
  73. [1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex64
  74. )
  75. complex128 = np.array(
  76. [1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex128
  77. )
  78. df = DataFrame(
  79. {
  80. "A": [1, 2, 3, 4],
  81. "B": ["a", "b", "c", "d"],
  82. "C": complex64,
  83. "D": complex128,
  84. "E": [1.0, 2.0, 3.0, 4.0],
  85. },
  86. index=list("abcd"),
  87. )
  88. with ensure_clean_store(setup_path) as store:
  89. store.append("df", df, data_columns=["A", "B"])
  90. result = store.select("df", where="A>2")
  91. tm.assert_frame_equal(df.loc[df.A > 2], result)
  92. path = tmp_path / setup_path
  93. df.to_hdf(path, "df", format="table")
  94. reread = read_hdf(path, "df")
  95. tm.assert_frame_equal(df, reread)
  96. def test_complex_across_dimensions_fixed(tmp_path, setup_path):
  97. with catch_warnings(record=True):
  98. complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j])
  99. s = Series(complex128, index=list("abcd"))
  100. df = DataFrame({"A": s, "B": s})
  101. objs = [s, df]
  102. comps = [tm.assert_series_equal, tm.assert_frame_equal]
  103. for obj, comp in zip(objs, comps):
  104. path = tmp_path / setup_path
  105. obj.to_hdf(path, "obj", format="fixed")
  106. reread = read_hdf(path, "obj")
  107. comp(obj, reread)
  108. def test_complex_across_dimensions(tmp_path, setup_path):
  109. complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j])
  110. s = Series(complex128, index=list("abcd"))
  111. df = DataFrame({"A": s, "B": s})
  112. with catch_warnings(record=True):
  113. objs = [df]
  114. comps = [tm.assert_frame_equal]
  115. for obj, comp in zip(objs, comps):
  116. path = tmp_path / setup_path
  117. obj.to_hdf(path, "obj", format="table")
  118. reread = read_hdf(path, "obj")
  119. comp(obj, reread)
  120. def test_complex_indexing_error(setup_path):
  121. complex128 = np.array(
  122. [1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex128
  123. )
  124. df = DataFrame(
  125. {"A": [1, 2, 3, 4], "B": ["a", "b", "c", "d"], "C": complex128},
  126. index=list("abcd"),
  127. )
  128. msg = (
  129. "Columns containing complex values can be stored "
  130. "but cannot be indexed when using table format. "
  131. "Either use fixed format, set index=False, "
  132. "or do not include the columns containing complex "
  133. "values to data_columns when initializing the table."
  134. )
  135. with ensure_clean_store(setup_path) as store:
  136. with pytest.raises(TypeError, match=msg):
  137. store.append("df", df, data_columns=["C"])
  138. def test_complex_series_error(tmp_path, setup_path):
  139. complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j])
  140. s = Series(complex128, index=list("abcd"))
  141. msg = (
  142. "Columns containing complex values can be stored "
  143. "but cannot be indexed when using table format. "
  144. "Either use fixed format, set index=False, "
  145. "or do not include the columns containing complex "
  146. "values to data_columns when initializing the table."
  147. )
  148. path = tmp_path / setup_path
  149. with pytest.raises(TypeError, match=msg):
  150. s.to_hdf(path, "obj", format="t")
  151. path = tmp_path / setup_path
  152. s.to_hdf(path, "obj", format="t", index=False)
  153. reread = read_hdf(path, "obj")
  154. tm.assert_series_equal(s, reread)
  155. def test_complex_append(setup_path):
  156. df = DataFrame(
  157. {"a": np.random.randn(100).astype(np.complex128), "b": np.random.randn(100)}
  158. )
  159. with ensure_clean_store(setup_path) as store:
  160. store.append("df", df, data_columns=["b"])
  161. store.append("df", df)
  162. result = store.select("df")
  163. tm.assert_frame_equal(pd.concat([df, df], axis=0), result)