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