MCPcopy
hub / github.com/pandas-dev/pandas / test_read_column

Function test_read_column

pandas/tests/io/pytables/test_read.py:65–141  ·  view source on GitHub ↗
(temp_hdfstore)

Source from the content-addressed store, hash-verified

63
64
65def test_read_column(temp_hdfstore):
66 df = DataFrame(
67 np.random.default_rng(2).standard_normal((10, 4)),
68 columns=Index(list("ABCD")),
69 index=date_range("2000-01-01", periods=10, freq="B"),
70 )
71
72 # GH 17912
73 # HDFStore.select_column should raise a KeyError
74 # exception if the key is not a valid store
75 with pytest.raises(KeyError, match="No object named df in the file"):
76 temp_hdfstore.select_column("df", "index")
77
78 temp_hdfstore.append("df", df)
79 # error
80 with pytest.raises(
81 KeyError, match=re.escape("'column [foo] not found in the table'")
82 ):
83 temp_hdfstore.select_column("df", "foo")
84
85 msg = re.escape("select_column() got an unexpected keyword argument 'where'")
86 with pytest.raises(TypeError, match=msg):
87 temp_hdfstore.select_column("df", "index", where=["index>5"])
88
89 # valid
90 result = temp_hdfstore.select_column("df", "index")
91 tm.assert_almost_equal(result.values, Series(df.index).values)
92 assert isinstance(result, Series)
93
94 # not a data indexable column
95 msg = re.escape(
96 "column [values_block_0] can not be extracted individually; "
97 "it is not data indexable"
98 )
99 with pytest.raises(ValueError, match=msg):
100 temp_hdfstore.select_column("df", "values_block_0")
101
102 # a data column
103 df2 = df.copy()
104 df2["string"] = "foo"
105 temp_hdfstore.append("df2", df2, data_columns=["string"])
106 result = temp_hdfstore.select_column("df2", "string")
107 tm.assert_almost_equal(result.values, df2["string"].values)
108
109 # a data column with NaNs, result excludes the NaNs
110 df3 = df.copy()
111 df3["string"] = "foo"
112 df3.loc[df3.index[4:6], "string"] = np.nan
113 temp_hdfstore.append("df3", df3, data_columns=["string"])
114 result = temp_hdfstore.select_column("df3", "string")
115 tm.assert_almost_equal(result.values, df3["string"].values)
116
117 # start/stop
118 result = temp_hdfstore.select_column("df3", "string", start=2)
119 tm.assert_almost_equal(result.values, df3["string"].values[2:])
120
121 result = temp_hdfstore.select_column("df3", "string", start=-2)
122 tm.assert_almost_equal(result.values, df3["string"].values[-2:])

Callers

nothing calls this directly

Calls 8

DataFrameClass · 0.90
IndexClass · 0.90
date_rangeFunction · 0.90
SeriesClass · 0.90
select_columnMethod · 0.80
raisesMethod · 0.45
appendMethod · 0.45
copyMethod · 0.45

Tested by

no test coverage detected