test_complex.py 5.8 KB

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