Skip to content

Commit 150cae9

Browse files
jbrockmendeljreback
authored andcommitted
CLN: assorted pytables cleanups, remove unwanted assertion (pandas-dev#29982)
1 parent 46e9f5b commit 150cae9

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

pandas/io/pytables.py

+34-33
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,6 @@ def read_hdf(
361361
>>> df.to_hdf('./store.h5', 'data')
362362
>>> reread = pd.read_hdf('./store.h5')
363363
"""
364-
assert not kwargs, kwargs
365-
# NB: in principle more kwargs could be passed to HDFStore, but in
366-
# tests none are.
367364

368365
if mode not in ["r", "r+", "a"]:
369366
raise ValueError(
@@ -500,13 +497,14 @@ class HDFStore:
500497
"""
501498

502499
_handle: Optional["File"]
500+
_mode: str
503501
_complevel: int
504502
_fletcher32: bool
505503

506504
def __init__(
507505
self,
508506
path,
509-
mode=None,
507+
mode: str = "a",
510508
complevel: Optional[int] = None,
511509
complib=None,
512510
fletcher32: bool = False,
@@ -837,16 +835,24 @@ def select_as_coordinates(
837835
raise TypeError("can only read_coordinates with a table")
838836
return tbl.read_coordinates(where=where, start=start, stop=stop)
839837

840-
def select_column(self, key: str, column: str, **kwargs):
838+
def select_column(
839+
self,
840+
key: str,
841+
column: str,
842+
start: Optional[int] = None,
843+
stop: Optional[int] = None,
844+
):
841845
"""
842846
return a single column from the table. This is generally only useful to
843847
select an indexable
844848
845849
Parameters
846850
----------
847851
key : str
848-
column: str
852+
column : str
849853
The column of interest.
854+
start : int or None, default None
855+
stop : int or None, default None
850856
851857
Raises
852858
------
@@ -859,7 +865,7 @@ def select_column(self, key: str, column: str, **kwargs):
859865
tbl = self.get_storer(key)
860866
if not isinstance(tbl, Table):
861867
raise TypeError("can only read_column with a table")
862-
return tbl.read_column(column=column, **kwargs)
868+
return tbl.read_column(column=column, start=start, stop=stop)
863869

864870
def select_as_multiple(
865871
self,
@@ -2582,9 +2588,9 @@ class Fixed:
25822588
25832589
Parameters
25842590
----------
2585-
2586-
parent : my parent HDFStore
2587-
group : the group node where the table resides
2591+
parent : HDFStore
2592+
group : Node
2593+
The group node where the table resides.
25882594
"""
25892595

25902596
pandas_kind: str
@@ -2871,7 +2877,7 @@ def read_index(
28712877
return self.read_multi_index(key, start=start, stop=stop)
28722878
elif variety == "regular":
28732879
node = getattr(self.group, key)
2874-
_, index = self.read_index_node(node, start=start, stop=stop)
2880+
index = self.read_index_node(node, start=start, stop=stop)
28752881
return index
28762882
else: # pragma: no cover
28772883
raise TypeError(f"unrecognized index variety: {variety}")
@@ -2931,13 +2937,13 @@ def read_multi_index(
29312937

29322938
levels = []
29332939
codes = []
2934-
names = []
2940+
names: List[Optional[Hashable]] = []
29352941
for i in range(nlevels):
29362942
level_key = f"{key}_level{i}"
29372943
node = getattr(self.group, level_key)
2938-
name, lev = self.read_index_node(node, start=start, stop=stop)
2944+
lev = self.read_index_node(node, start=start, stop=stop)
29392945
levels.append(lev)
2940-
names.append(name)
2946+
names.append(lev.name)
29412947

29422948
label_key = f"{key}_label{i}"
29432949
level_codes = self.read_array(label_key, start=start, stop=stop)
@@ -2949,7 +2955,7 @@ def read_multi_index(
29492955

29502956
def read_index_node(
29512957
self, node: "Node", start: Optional[int] = None, stop: Optional[int] = None
2952-
):
2958+
) -> Index:
29532959
data = node[start:stop]
29542960
# If the index was an empty array write_array_empty() will
29552961
# have written a sentinel. Here we relace it with the original.
@@ -2997,7 +3003,7 @@ def read_index_node(
29973003

29983004
index.name = name
29993005

3000-
return name, index
3006+
return index
30013007

30023008
def write_array_empty(self, key: str, value):
30033009
""" write a 0-len array """
@@ -3131,7 +3137,6 @@ def write(self, obj, **kwargs):
31313137

31323138
class BlockManagerFixed(GenericFixed):
31333139
attributes = ["ndim", "nblocks"]
3134-
is_shape_reversed = False
31353140

31363141
nblocks: int
31373142

@@ -3158,10 +3163,6 @@ def shape(self):
31583163

31593164
shape.append(items)
31603165

3161-
# hacky - this works for frames, but is reversed for panels
3162-
if self.is_shape_reversed:
3163-
shape = shape[::-1]
3164-
31653166
return shape
31663167
except AttributeError:
31673168
return None
@@ -3259,7 +3260,6 @@ class Table(Fixed):
32593260
table_type: str
32603261
levels = 1
32613262
is_table = True
3262-
is_shape_reversed = False
32633263

32643264
index_axes: List[IndexCol]
32653265
non_index_axes: List[Tuple[int, Any]]
@@ -3302,7 +3302,7 @@ def __repr__(self) -> str:
33023302
f"ncols->{self.ncols},indexers->[{jindex_axes}]{dc})"
33033303
)
33043304

3305-
def __getitem__(self, c):
3305+
def __getitem__(self, c: str):
33063306
""" return the axis for c """
33073307
for a in self.axes:
33083308
if c == a.name:
@@ -3345,10 +3345,6 @@ def is_multi_index(self) -> bool:
33453345
"""the levels attribute is 1 or a list in the case of a multi-index"""
33463346
return isinstance(self.levels, list)
33473347

3348-
def validate_metadata(self, existing):
3349-
""" create / validate metadata """
3350-
self.metadata = [c.name for c in self.values_axes if c.metadata is not None]
3351-
33523348
def validate_multiindex(self, obj):
33533349
"""validate that we can store the multi-index; reset and return the
33543350
new object
@@ -3651,8 +3647,8 @@ def read_axes(
36513647
Parameters
36523648
----------
36533649
where : ???
3654-
start: int or None, default None
3655-
stop: int or None, default None
3650+
start : int or None, default None
3651+
stop : int or None, default None
36563652
36573653
Returns
36583654
-------
@@ -3946,7 +3942,7 @@ def get_blk_items(mgr, blocks):
39463942
self.validate_min_itemsize(min_itemsize)
39473943

39483944
# validate our metadata
3949-
self.validate_metadata(existing_table)
3945+
self.metadata = [c.name for c in self.values_axes if c.metadata is not None]
39503946

39513947
# validate the axes if we have an existing table
39523948
if validate:
@@ -4122,7 +4118,13 @@ class WORMTable(Table):
41224118

41234119
table_type = "worm"
41244120

4125-
def read(self, **kwargs):
4121+
def read(
4122+
self,
4123+
where=None,
4124+
columns=None,
4125+
start: Optional[int] = None,
4126+
stop: Optional[int] = None,
4127+
):
41264128
""" read the indices and the indexing array, calculate offset rows and
41274129
return """
41284130
raise NotImplementedError("WORMTable needs to implement read")
@@ -4479,8 +4481,7 @@ def write(self, obj, data_columns=None, **kwargs):
44794481
""" we are going to write this as a frame table """
44804482
if not isinstance(obj, DataFrame):
44814483
name = obj.name or "values"
4482-
obj = DataFrame({name: obj}, index=obj.index)
4483-
obj.columns = [name]
4484+
obj = obj.to_frame(name)
44844485
return super().write(obj=obj, data_columns=obj.columns.tolist(), **kwargs)
44854486

44864487
def read(

0 commit comments

Comments
 (0)