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