@@ -75,81 +75,75 @@ def test_read_missing_key_opened_store(tmp_path, setup_path):
7575def test_read_column (setup_path ):
7676 df = DataFrame (
7777 np .random .default_rng (2 ).standard_normal ((10 , 4 )),
78- columns = Index (
79- list ("ABCD" ),
80- ),
78+ columns = Index (list ("ABCD" )),
8179 index = date_range ("2000-01-01" , periods = 10 , freq = "B" ),
8280 )
8381
8482 with ensure_clean_store (setup_path ) as store :
8583 _maybe_remove (store , "df" )
8684
87- # # GH 17912
88- # # HDFStore.select_column should raise a KeyError
89- # # exception if the key is not a valid store
90- # with pytest.raises(KeyError, match="No object named df in the file"):
91- # store.select_column("df", "index")
92- #
93- # store.append("df", df)
94- # # error
95- # with pytest.raises(
96- # KeyError, match=re.escape("'column [foo] not found in the table'")
97- # ):
98- # store.select_column("df", "foo")
99- #
100- # msg = re.escape("select_column() got an unexpected keyword argument 'where'")
101- # with pytest.raises(TypeError, match=msg):
102- # store.select_column("df", "index", where=["index>5"])
103- #
104- # # valid
105- # result = store.select_column("df", "index")
106- # tm.assert_almost_equal(result.values, Series(df.index).values)
107- # assert isinstance(result, Series)
108- #
109- # # not a data indexable column
110- # msg = re.escape(
111- # "column [values_block_0] can not be extracted individually; "
112- # "it is not data indexable"
113- # )
114- # with pytest.raises(ValueError, match=msg):
115- # store.select_column("df", "values_block_0")
116- #
117- # # a data column
118- # df2 = df.copy()
119- # df2["string"] = "foo"
120- # store.append("df2", df2, data_columns=["string"])
121- # result = store.select_column("df2", "string")
122- # tm.assert_almost_equal(result.values, df2["string"].values)
85+ # GH 17912
86+ # HDFStore.select_column should raise a KeyError
87+ # exception if the key is not a valid store
88+ with pytest .raises (KeyError , match = "No object named df in the file" ):
89+ store .select_column ("df" , "index" )
90+
91+ store .append ("df" , df )
92+ # error
93+ with pytest .raises (
94+ KeyError , match = re .escape ("'column [foo] not found in the table'" )
95+ ):
96+ store .select_column ("df" , "foo" )
97+
98+ msg = re .escape ("select_column() got an unexpected keyword argument 'where'" )
99+ with pytest .raises (TypeError , match = msg ):
100+ store .select_column ("df" , "index" , where = ["index>5" ])
101+
102+ # valid
103+ result = store .select_column ("df" , "index" )
104+ tm .assert_almost_equal (result .values , Series (df .index ).values )
105+ assert isinstance (result , Series )
106+
107+ # not a data indexable column
108+ msg = re .escape (
109+ "column [values_block_0] can not be extracted individually; "
110+ "it is not data indexable"
111+ )
112+ with pytest .raises (ValueError , match = msg ):
113+ store .select_column ("df" , "values_block_0" )
114+
115+ # a data column
116+ df2 = df .copy ()
117+ df2 ["string" ] = "foo"
118+ store .append ("df2" , df2 , data_columns = ["string" ])
119+ result = store .select_column ("df2" , "string" )
120+ tm .assert_almost_equal (result .values , df2 ["string" ].values )
123121
124122 # a data column with NaNs, result excludes the NaNs
125123 df3 = df .copy ()
126124 df3 ["string" ] = "foo"
127125 df3 .loc [df3 .index [4 :6 ], "string" ] = np .nan
128126 store .append ("df3" , df3 , data_columns = ["string" ])
129- # result = store.select_column("df3", "string")
130- # tm.assert_almost_equal(result.values, df3["string"].values)
131- #
132- # # start/stop
133- # result = store.select_column("df3", "string", start=2)
134- # tm.assert_almost_equal(result.values, df3["string"].values[2:])
135- #
136- # result = store.select_column("df3", "string", start=-2)
137- # tm.assert_almost_equal(result.values, df3["string"].values[-2:])
138- #
139- # result = store.select_column("df3", "string", stop=2)
140- # tm.assert_almost_equal(result.values, df3["string"].values[:2])
141- #
142- # result = store.select_column("df3", "string", stop=-2)
143- # tm.assert_almost_equal(result.values, df3["string"].values[:-2])
144- #
145- # result = store.select_column("df3", "string", start=2, stop=-2)
146- # tm.assert_almost_equal(result.values, df3["string"].values[2:-2])
147-
148- print (df3 )
149- print (df3 ["string" ].values )
127+ result = store .select_column ("df3" , "string" )
128+ tm .assert_almost_equal (result .values , df3 ["string" ].values )
129+
130+ # start/stop
131+ result = store .select_column ("df3" , "string" , start = 2 )
132+ tm .assert_almost_equal (result .values , df3 ["string" ].values [2 :])
133+
134+ result = store .select_column ("df3" , "string" , start = - 2 )
135+ tm .assert_almost_equal (result .values , df3 ["string" ].values [- 2 :])
136+
137+ result = store .select_column ("df3" , "string" , stop = 2 )
138+ tm .assert_almost_equal (result .values , df3 ["string" ].values [:2 ])
139+
140+ result = store .select_column ("df3" , "string" , stop = - 2 )
141+ tm .assert_almost_equal (result .values , df3 ["string" ].values [:- 2 ])
142+
143+ result = store .select_column ("df3" , "string" , start = 2 , stop = - 2 )
144+ tm .assert_almost_equal (result .values , df3 ["string" ].values [2 :- 2 ])
145+
150146 result = store .select_column ("df3" , "string" , start = - 2 , stop = 2 )
151- print (result )
152- print (result .values )
153147 tm .assert_almost_equal (result .values , df3 ["string" ].values [- 2 :2 ])
154148
155149 # GH 10392 - make sure column name is preserved
0 commit comments