|
32 | 32 | )
|
33 | 33 |
|
34 | 34 |
|
35 |
| -class TestHDFComplexValues: |
36 |
| - # GH10447 |
37 |
| - |
38 |
| - def test_complex_fixed(self, setup_path): |
39 |
| - df = DataFrame( |
40 |
| - np.random.rand(4, 5).astype(np.complex64), |
41 |
| - index=list("abcd"), |
42 |
| - columns=list("ABCDE"), |
43 |
| - ) |
44 |
| - |
45 |
| - with ensure_clean_path(setup_path) as path: |
46 |
| - df.to_hdf(path, "df") |
47 |
| - reread = read_hdf(path, "df") |
48 |
| - assert_frame_equal(df, reread) |
49 |
| - |
50 |
| - df = DataFrame( |
51 |
| - np.random.rand(4, 5).astype(np.complex128), |
52 |
| - index=list("abcd"), |
53 |
| - columns=list("ABCDE"), |
54 |
| - ) |
55 |
| - with ensure_clean_path(setup_path) as path: |
56 |
| - df.to_hdf(path, "df") |
57 |
| - reread = read_hdf(path, "df") |
58 |
| - assert_frame_equal(df, reread) |
59 |
| - |
60 |
| - def test_complex_table(self, setup_path): |
61 |
| - df = DataFrame( |
62 |
| - np.random.rand(4, 5).astype(np.complex64), |
63 |
| - index=list("abcd"), |
64 |
| - columns=list("ABCDE"), |
65 |
| - ) |
66 |
| - |
67 |
| - with ensure_clean_path(setup_path) as path: |
68 |
| - df.to_hdf(path, "df", format="table") |
69 |
| - reread = read_hdf(path, "df") |
70 |
| - assert_frame_equal(df, reread) |
71 |
| - |
72 |
| - df = DataFrame( |
73 |
| - np.random.rand(4, 5).astype(np.complex128), |
74 |
| - index=list("abcd"), |
75 |
| - columns=list("ABCDE"), |
76 |
| - ) |
77 |
| - |
78 |
| - with ensure_clean_path(setup_path) as path: |
79 |
| - df.to_hdf(path, "df", format="table", mode="w") |
80 |
| - reread = read_hdf(path, "df") |
81 |
| - assert_frame_equal(df, reread) |
82 |
| - |
83 |
| - @xfail_non_writeable |
84 |
| - def test_complex_mixed_fixed(self, setup_path): |
85 |
| - complex64 = np.array( |
86 |
| - [1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex64 |
87 |
| - ) |
88 |
| - complex128 = np.array( |
89 |
| - [1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex128 |
90 |
| - ) |
91 |
| - df = DataFrame( |
92 |
| - { |
93 |
| - "A": [1, 2, 3, 4], |
94 |
| - "B": ["a", "b", "c", "d"], |
95 |
| - "C": complex64, |
96 |
| - "D": complex128, |
97 |
| - "E": [1.0, 2.0, 3.0, 4.0], |
98 |
| - }, |
99 |
| - index=list("abcd"), |
100 |
| - ) |
101 |
| - with ensure_clean_path(setup_path) as path: |
102 |
| - df.to_hdf(path, "df") |
103 |
| - reread = read_hdf(path, "df") |
104 |
| - assert_frame_equal(df, reread) |
105 |
| - |
106 |
| - def test_complex_mixed_table(self, setup_path): |
107 |
| - complex64 = np.array( |
108 |
| - [1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex64 |
109 |
| - ) |
110 |
| - complex128 = np.array( |
111 |
| - [1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex128 |
112 |
| - ) |
113 |
| - df = DataFrame( |
114 |
| - { |
115 |
| - "A": [1, 2, 3, 4], |
116 |
| - "B": ["a", "b", "c", "d"], |
117 |
| - "C": complex64, |
118 |
| - "D": complex128, |
119 |
| - "E": [1.0, 2.0, 3.0, 4.0], |
120 |
| - }, |
121 |
| - index=list("abcd"), |
122 |
| - ) |
123 |
| - |
124 |
| - with ensure_clean_store(setup_path) as store: |
125 |
| - store.append("df", df, data_columns=["A", "B"]) |
126 |
| - result = store.select("df", where="A>2") |
127 |
| - assert_frame_equal(df.loc[df.A > 2], result) |
128 |
| - |
129 |
| - with ensure_clean_path(setup_path) as path: |
130 |
| - df.to_hdf(path, "df", format="table") |
131 |
| - reread = read_hdf(path, "df") |
132 |
| - assert_frame_equal(df, reread) |
133 |
| - |
134 |
| - def test_complex_across_dimensions_fixed(self, setup_path): |
135 |
| - with catch_warnings(record=True): |
136 |
| - complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j]) |
137 |
| - s = Series(complex128, index=list("abcd")) |
138 |
| - df = DataFrame({"A": s, "B": s}) |
139 |
| - |
140 |
| - objs = [s, df] |
141 |
| - comps = [tm.assert_series_equal, tm.assert_frame_equal] |
142 |
| - for obj, comp in zip(objs, comps): |
143 |
| - with ensure_clean_path(setup_path) as path: |
144 |
| - obj.to_hdf(path, "obj", format="fixed") |
145 |
| - reread = read_hdf(path, "obj") |
146 |
| - comp(obj, reread) |
147 |
| - |
148 |
| - def test_complex_across_dimensions(self, setup_path): |
| 35 | +# GH10447 |
| 36 | + |
| 37 | +def test_complex_fixed(setup_path): |
| 38 | + df = DataFrame( |
| 39 | + np.random.rand(4, 5).astype(np.complex64), |
| 40 | + index=list("abcd"), |
| 41 | + columns=list("ABCDE"), |
| 42 | + ) |
| 43 | + |
| 44 | + with ensure_clean_path(setup_path) as path: |
| 45 | + df.to_hdf(path, "df") |
| 46 | + reread = read_hdf(path, "df") |
| 47 | + assert_frame_equal(df, reread) |
| 48 | + |
| 49 | + df = DataFrame( |
| 50 | + np.random.rand(4, 5).astype(np.complex128), |
| 51 | + index=list("abcd"), |
| 52 | + columns=list("ABCDE"), |
| 53 | + ) |
| 54 | + with ensure_clean_path(setup_path) as path: |
| 55 | + df.to_hdf(path, "df") |
| 56 | + reread = read_hdf(path, "df") |
| 57 | + assert_frame_equal(df, reread) |
| 58 | + |
| 59 | +def test_complex_table(setup_path): |
| 60 | + df = DataFrame( |
| 61 | + np.random.rand(4, 5).astype(np.complex64), |
| 62 | + index=list("abcd"), |
| 63 | + columns=list("ABCDE"), |
| 64 | + ) |
| 65 | + |
| 66 | + with ensure_clean_path(setup_path) as path: |
| 67 | + df.to_hdf(path, "df", format="table") |
| 68 | + reread = read_hdf(path, "df") |
| 69 | + assert_frame_equal(df, reread) |
| 70 | + |
| 71 | + df = DataFrame( |
| 72 | + np.random.rand(4, 5).astype(np.complex128), |
| 73 | + index=list("abcd"), |
| 74 | + columns=list("ABCDE"), |
| 75 | + ) |
| 76 | + |
| 77 | + with ensure_clean_path(setup_path) as path: |
| 78 | + df.to_hdf(path, "df", format="table", mode="w") |
| 79 | + reread = read_hdf(path, "df") |
| 80 | + assert_frame_equal(df, reread) |
| 81 | + |
| 82 | +@xfail_non_writeable |
| 83 | +def test_complex_mixed_fixed(setup_path): |
| 84 | + complex64 = np.array( |
| 85 | + [1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex64 |
| 86 | + ) |
| 87 | + complex128 = np.array( |
| 88 | + [1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex128 |
| 89 | + ) |
| 90 | + df = DataFrame( |
| 91 | + { |
| 92 | + "A": [1, 2, 3, 4], |
| 93 | + "B": ["a", "b", "c", "d"], |
| 94 | + "C": complex64, |
| 95 | + "D": complex128, |
| 96 | + "E": [1.0, 2.0, 3.0, 4.0], |
| 97 | + }, |
| 98 | + index=list("abcd"), |
| 99 | + ) |
| 100 | + with ensure_clean_path(setup_path) as path: |
| 101 | + df.to_hdf(path, "df") |
| 102 | + reread = read_hdf(path, "df") |
| 103 | + assert_frame_equal(df, reread) |
| 104 | + |
| 105 | +def test_complex_mixed_table(setup_path): |
| 106 | + complex64 = np.array( |
| 107 | + [1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex64 |
| 108 | + ) |
| 109 | + complex128 = np.array( |
| 110 | + [1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex128 |
| 111 | + ) |
| 112 | + df = DataFrame( |
| 113 | + { |
| 114 | + "A": [1, 2, 3, 4], |
| 115 | + "B": ["a", "b", "c", "d"], |
| 116 | + "C": complex64, |
| 117 | + "D": complex128, |
| 118 | + "E": [1.0, 2.0, 3.0, 4.0], |
| 119 | + }, |
| 120 | + index=list("abcd"), |
| 121 | + ) |
| 122 | + |
| 123 | + with ensure_clean_store(setup_path) as store: |
| 124 | + store.append("df", df, data_columns=["A", "B"]) |
| 125 | + result = store.select("df", where="A>2") |
| 126 | + assert_frame_equal(df.loc[df.A > 2], result) |
| 127 | + |
| 128 | + with ensure_clean_path(setup_path) as path: |
| 129 | + df.to_hdf(path, "df", format="table") |
| 130 | + reread = read_hdf(path, "df") |
| 131 | + assert_frame_equal(df, reread) |
| 132 | + |
| 133 | +def test_complex_across_dimensions_fixed(setup_path): |
| 134 | + with catch_warnings(record=True): |
149 | 135 | complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j])
|
150 | 136 | s = Series(complex128, index=list("abcd"))
|
151 | 137 | df = DataFrame({"A": s, "B": s})
|
152 | 138 |
|
153 |
| - with catch_warnings(record=True): |
154 |
| - |
155 |
| - objs = [df] |
156 |
| - comps = [tm.assert_frame_equal] |
157 |
| - for obj, comp in zip(objs, comps): |
158 |
| - with ensure_clean_path(setup_path) as path: |
159 |
| - obj.to_hdf(path, "obj", format="table") |
160 |
| - reread = read_hdf(path, "obj") |
161 |
| - comp(obj, reread) |
162 |
| - |
163 |
| - def test_complex_indexing_error(self, setup_path): |
164 |
| - complex128 = np.array( |
165 |
| - [1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex128 |
166 |
| - ) |
167 |
| - df = DataFrame( |
168 |
| - {"A": [1, 2, 3, 4], "B": ["a", "b", "c", "d"], "C": complex128}, |
169 |
| - index=list("abcd"), |
170 |
| - ) |
171 |
| - with ensure_clean_store(setup_path) as store: |
172 |
| - with pytest.raises(TypeError): |
173 |
| - store.append("df", df, data_columns=["C"]) |
174 |
| - |
175 |
| - def test_complex_series_error(self, setup_path): |
176 |
| - complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j]) |
177 |
| - s = Series(complex128, index=list("abcd")) |
178 |
| - |
179 |
| - with ensure_clean_path(setup_path) as path: |
180 |
| - with pytest.raises(TypeError): |
181 |
| - s.to_hdf(path, "obj", format="t") |
182 |
| - |
183 |
| - with ensure_clean_path(setup_path) as path: |
184 |
| - s.to_hdf(path, "obj", format="t", index=False) |
185 |
| - reread = read_hdf(path, "obj") |
186 |
| - tm.assert_series_equal(s, reread) |
187 |
| - |
188 |
| - def test_complex_append(self, setup_path): |
189 |
| - df = DataFrame( |
190 |
| - {"a": np.random.randn(100).astype(np.complex128), "b": np.random.randn(100)} |
191 |
| - ) |
192 |
| - |
193 |
| - with ensure_clean_store(setup_path) as store: |
194 |
| - store.append("df", df, data_columns=["b"]) |
195 |
| - store.append("df", df) |
196 |
| - result = store.select("df") |
197 |
| - assert_frame_equal(pd.concat([df, df], 0), result) |
| 139 | + objs = [s, df] |
| 140 | + comps = [tm.assert_series_equal, tm.assert_frame_equal] |
| 141 | + for obj, comp in zip(objs, comps): |
| 142 | + with ensure_clean_path(setup_path) as path: |
| 143 | + obj.to_hdf(path, "obj", format="fixed") |
| 144 | + reread = read_hdf(path, "obj") |
| 145 | + comp(obj, reread) |
| 146 | + |
| 147 | +def test_complex_across_dimensions(setup_path): |
| 148 | + complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j]) |
| 149 | + s = Series(complex128, index=list("abcd")) |
| 150 | + df = DataFrame({"A": s, "B": s}) |
| 151 | + |
| 152 | + with catch_warnings(record=True): |
| 153 | + |
| 154 | + objs = [df] |
| 155 | + comps = [tm.assert_frame_equal] |
| 156 | + for obj, comp in zip(objs, comps): |
| 157 | + with ensure_clean_path(setup_path) as path: |
| 158 | + obj.to_hdf(path, "obj", format="table") |
| 159 | + reread = read_hdf(path, "obj") |
| 160 | + comp(obj, reread) |
| 161 | + |
| 162 | +def test_complex_indexing_error(setup_path): |
| 163 | + complex128 = np.array( |
| 164 | + [1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j], dtype=np.complex128 |
| 165 | + ) |
| 166 | + df = DataFrame( |
| 167 | + {"A": [1, 2, 3, 4], "B": ["a", "b", "c", "d"], "C": complex128}, |
| 168 | + index=list("abcd"), |
| 169 | + ) |
| 170 | + with ensure_clean_store(setup_path) as store: |
| 171 | + with pytest.raises(TypeError): |
| 172 | + store.append("df", df, data_columns=["C"]) |
| 173 | + |
| 174 | +def test_complex_series_error(setup_path): |
| 175 | + complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j]) |
| 176 | + s = Series(complex128, index=list("abcd")) |
| 177 | + |
| 178 | + with ensure_clean_path(setup_path) as path: |
| 179 | + with pytest.raises(TypeError): |
| 180 | + s.to_hdf(path, "obj", format="t") |
| 181 | + |
| 182 | + with ensure_clean_path(setup_path) as path: |
| 183 | + s.to_hdf(path, "obj", format="t", index=False) |
| 184 | + reread = read_hdf(path, "obj") |
| 185 | + tm.assert_series_equal(s, reread) |
| 186 | + |
| 187 | +def test_complex_append(setup_path): |
| 188 | + df = DataFrame( |
| 189 | + {"a": np.random.randn(100).astype(np.complex128), "b": np.random.randn(100)} |
| 190 | + ) |
| 191 | + |
| 192 | + with ensure_clean_store(setup_path) as store: |
| 193 | + store.append("df", df, data_columns=["b"]) |
| 194 | + store.append("df", df) |
| 195 | + result = store.select("df") |
| 196 | + assert_frame_equal(pd.concat([df, df], 0), result) |
0 commit comments