@@ -131,18 +131,18 @@ def compat_assert_produces_warning(w,f):
131
131
f ()
132
132
133
133
134
- class TestHDFStore (tm .TestCase ):
134
+ class Base (tm .TestCase ):
135
135
136
136
@classmethod
137
137
def setUpClass (cls ):
138
- super (TestHDFStore , cls ).setUpClass ()
138
+ super (Base , cls ).setUpClass ()
139
139
140
140
# Pytables 3.0.0 deprecates lots of things
141
141
tm .reset_testing_mode ()
142
142
143
143
@classmethod
144
144
def tearDownClass (cls ):
145
- super (TestHDFStore , cls ).tearDownClass ()
145
+ super (Base , cls ).tearDownClass ()
146
146
147
147
# Pytables 3.0.0 deprecates lots of things
148
148
tm .set_testing_mode ()
@@ -155,6 +155,9 @@ def setUp(self):
155
155
def tearDown (self ):
156
156
pass
157
157
158
+
159
+ class TestHDFStore (Base ):
160
+
158
161
def test_factory_fun (self ):
159
162
path = create_tempfile (self .path )
160
163
try :
@@ -4743,6 +4746,146 @@ def test_read_nokey(self):
4743
4746
df .to_hdf (path , 'df2' , mode = 'a' )
4744
4747
self .assertRaises (ValueError , read_hdf , path )
4745
4748
4749
+
4750
+ class TestHDFComplexValues (Base ):
4751
+ # GH10447
4752
+ def test_complex_fixed (self ):
4753
+ df = DataFrame (np .random .rand (4 , 5 ).astype (np .complex64 ),
4754
+ index = list ('abcd' ),
4755
+ columns = list ('ABCDE' ))
4756
+
4757
+ with ensure_clean_path (self .path ) as path :
4758
+ df .to_hdf (path , 'df' )
4759
+ reread = read_hdf (path , 'df' )
4760
+ assert_frame_equal (df , reread )
4761
+
4762
+ df = DataFrame (np .random .rand (4 , 5 ).astype (np .complex128 ),
4763
+ index = list ('abcd' ),
4764
+ columns = list ('ABCDE' ))
4765
+ with ensure_clean_path (self .path ) as path :
4766
+ df .to_hdf (path , 'df' )
4767
+ reread = read_hdf (path , 'df' )
4768
+ assert_frame_equal (df , reread )
4769
+
4770
+ def test_complex_table (self ):
4771
+ df = DataFrame (np .random .rand (4 , 5 ).astype (np .complex64 ),
4772
+ index = list ('abcd' ),
4773
+ columns = list ('ABCDE' ))
4774
+
4775
+ with ensure_clean_path (self .path ) as path :
4776
+ df .to_hdf (path , 'df' , format = 'table' )
4777
+ reread = read_hdf (path , 'df' )
4778
+ assert_frame_equal (df , reread )
4779
+
4780
+ df = DataFrame (np .random .rand (4 , 5 ).astype (np .complex128 ),
4781
+ index = list ('abcd' ),
4782
+ columns = list ('ABCDE' ))
4783
+
4784
+ with ensure_clean_path (self .path ) as path :
4785
+ df .to_hdf (path , 'df' , format = 'table' , mode = 'w' )
4786
+ reread = read_hdf (path , 'df' )
4787
+ assert_frame_equal (df , reread )
4788
+
4789
+ def test_complex_mixed_fixed (self ):
4790
+ complex64 = np .array ([1.0 + 1.0j , 1.0 + 1.0j , 1.0 + 1.0j , 1.0 + 1.0j ], dtype = np .complex64 )
4791
+ complex128 = np .array ([1.0 + 1.0j , 1.0 + 1.0j , 1.0 + 1.0j , 1.0 + 1.0j ],
4792
+ dtype = np .complex128 )
4793
+ df = DataFrame ({'A' : [1 , 2 , 3 , 4 ],
4794
+ 'B' : ['a' , 'b' , 'c' , 'd' ],
4795
+ 'C' : complex64 ,
4796
+ 'D' : complex128 ,
4797
+ 'E' : [1.0 , 2.0 , 3.0 , 4.0 ]},
4798
+ index = list ('abcd' ))
4799
+ with ensure_clean_path (self .path ) as path :
4800
+ df .to_hdf (path , 'df' )
4801
+ reread = read_hdf (path , 'df' )
4802
+ assert_frame_equal (df , reread )
4803
+
4804
+ def test_complex_mixed_table (self ):
4805
+ complex64 = np .array ([1.0 + 1.0j , 1.0 + 1.0j , 1.0 + 1.0j , 1.0 + 1.0j ], dtype = np .complex64 )
4806
+ complex128 = np .array ([1.0 + 1.0j , 1.0 + 1.0j , 1.0 + 1.0j , 1.0 + 1.0j ],
4807
+ dtype = np .complex128 )
4808
+ df = DataFrame ({'A' : [1 , 2 , 3 , 4 ],
4809
+ 'B' : ['a' , 'b' , 'c' , 'd' ],
4810
+ 'C' : complex64 ,
4811
+ 'D' : complex128 ,
4812
+ 'E' : [1.0 , 2.0 , 3.0 , 4.0 ]},
4813
+ index = list ('abcd' ))
4814
+
4815
+ with ensure_clean_store (self .path ) as store :
4816
+ store .append ('df' , df , data_columns = ['A' , 'B' ])
4817
+ result = store .select ('df' , where = Term ('A>2' ))
4818
+ assert_frame_equal (df .loc [df .A > 2 ], result )
4819
+
4820
+ with ensure_clean_path (self .path ) as path :
4821
+ df .to_hdf (path , 'df' , format = 'table' )
4822
+ reread = read_hdf (path , 'df' )
4823
+ assert_frame_equal (df , reread )
4824
+
4825
+ def test_complex_across_dimensions_fixed (self ):
4826
+ complex128 = np .array ([1.0 + 1.0j , 1.0 + 1.0j , 1.0 + 1.0j , 1.0 + 1.0j ])
4827
+ s = Series (complex128 , index = list ('abcd' ))
4828
+ df = DataFrame ({'A' : s , 'B' : s })
4829
+ p = Panel ({'One' : df , 'Two' : df })
4830
+
4831
+ objs = [s , df , p ]
4832
+ comps = [tm .assert_series_equal , tm .assert_frame_equal ,
4833
+ tm .assert_panel_equal ]
4834
+ for obj , comp in zip (objs , comps ):
4835
+ with ensure_clean_path (self .path ) as path :
4836
+ obj .to_hdf (path , 'obj' , format = 'fixed' )
4837
+ reread = read_hdf (path , 'obj' )
4838
+ comp (obj , reread )
4839
+
4840
+ def test_complex_across_dimensions (self ):
4841
+ complex128 = np .array ([1.0 + 1.0j , 1.0 + 1.0j , 1.0 + 1.0j , 1.0 + 1.0j ])
4842
+ s = Series (complex128 , index = list ('abcd' ))
4843
+ df = DataFrame ({'A' : s , 'B' : s })
4844
+ p = Panel ({'One' : df , 'Two' : df })
4845
+ p4d = pd .Panel4D ({'i' : p , 'ii' : p })
4846
+
4847
+ objs = [df , p , p4d ]
4848
+ comps = [tm .assert_frame_equal , tm .assert_panel_equal ,
4849
+ tm .assert_panel4d_equal ]
4850
+ for obj , comp in zip (objs , comps ):
4851
+ with ensure_clean_path (self .path ) as path :
4852
+ obj .to_hdf (path , 'obj' , format = 'table' )
4853
+ reread = read_hdf (path , 'obj' )
4854
+ comp (obj , reread )
4855
+
4856
+ def test_complex_indexing_error (self ):
4857
+ complex128 = np .array ([1.0 + 1.0j , 1.0 + 1.0j , 1.0 + 1.0j , 1.0 + 1.0j ],
4858
+ dtype = np .complex128 )
4859
+ df = DataFrame ({'A' : [1 , 2 , 3 , 4 ],
4860
+ 'B' : ['a' , 'b' , 'c' , 'd' ],
4861
+ 'C' : complex128 },
4862
+ index = list ('abcd' ))
4863
+ with ensure_clean_store (self .path ) as store :
4864
+ self .assertRaises (TypeError , store .append , 'df' , df , data_columns = ['C' ])
4865
+
4866
+ def test_complex_series_error (self ):
4867
+ complex128 = np .array ([1.0 + 1.0j , 1.0 + 1.0j , 1.0 + 1.0j , 1.0 + 1.0j ])
4868
+ s = Series (complex128 , index = list ('abcd' ))
4869
+
4870
+ with ensure_clean_path (self .path ) as path :
4871
+ self .assertRaises (TypeError , s .to_hdf , path , 'obj' , format = 't' )
4872
+
4873
+ with ensure_clean_path (self .path ) as path :
4874
+ s .to_hdf (path , 'obj' , format = 't' , index = False )
4875
+ reread = read_hdf (path , 'obj' )
4876
+ tm .assert_series_equal (s , reread )
4877
+
4878
+ def test_complex_append (self ):
4879
+ df = DataFrame ({'a' : np .random .randn (100 ).astype (np .complex128 ),
4880
+ 'b' : np .random .randn (100 )})
4881
+
4882
+ with ensure_clean_store (self .path ) as store :
4883
+ store .append ('df' , df , data_columns = ['b' ])
4884
+ store .append ('df' , df )
4885
+ result = store .select ('df' )
4886
+ assert_frame_equal (pd .concat ([df , df ], 0 ), result )
4887
+
4888
+
4746
4889
def _test_sort (obj ):
4747
4890
if isinstance (obj , DataFrame ):
4748
4891
return obj .reindex (sorted (obj .index ))
0 commit comments