Skip to content

Commit d3ed97f

Browse files
committed
fix up Hashable refs and add tests
1 parent 755d069 commit d3ed97f

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

pandas-stubs/core/frame.pyi

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,24 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
437437
_str | npt.DTypeLike | Mapping[HashableT2, npt.DTypeLike] | None
438438
) = ...,
439439
) -> np.recarray: ...
440+
@overload
441+
def to_stata(
442+
self,
443+
path: FilePath | WriteBuffer[bytes],
444+
*,
445+
convert_dates: dict[HashableT1, StataDateFormat] | None = ...,
446+
write_index: _bool = ...,
447+
byteorder: Literal["<", ">", "little", "big"] | None = ...,
448+
time_stamp: dt.datetime | None = ...,
449+
data_label: _str | None = ...,
450+
variable_labels: dict[HashableT2, str] | None = ...,
451+
version: Literal[117, 118, 119],
452+
convert_strl: SequenceNotStr[Hashable] | None = ...,
453+
compression: CompressionOptions = ...,
454+
storage_options: StorageOptions = ...,
455+
value_labels: dict[Hashable, dict[float, str]] | None = ...,
456+
) -> None: ...
457+
@overload
440458
def to_stata(
441459
self,
442460
path: FilePath | WriteBuffer[bytes],
@@ -448,7 +466,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
448466
data_label: _str | None = ...,
449467
variable_labels: dict[HashableT2, str] | None = ...,
450468
version: Literal[114, 117, 118, 119] | None = ...,
451-
convert_strl: list[Hashable] | None = ...,
469+
convert_strl: None = ...,
452470
compression: CompressionOptions = ...,
453471
storage_options: StorageOptions = ...,
454472
value_labels: dict[Hashable, dict[float, str]] | None = ...,
@@ -498,7 +516,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
498516
def to_html(
499517
self,
500518
buf: FilePath | WriteBuffer[str],
501-
columns: list[Hashable] | Index | Series | None = ...,
519+
columns: SequenceNotStr[Hashable] | Index | Series | None = ...,
502520
col_space: ColspaceArgType | None = ...,
503521
header: _bool = ...,
504522
index: _bool = ...,
@@ -596,8 +614,8 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
596614
root_name: str = ...,
597615
row_name: str = ...,
598616
na_rep: str | None = ...,
599-
attr_cols: list[Hashable] | None = ...,
600-
elem_cols: list[Hashable] | None = ...,
617+
attr_cols: SequenceNotStr[Hashable] | None = ...,
618+
elem_cols: SequenceNotStr[Hashable] | None = ...,
601619
namespaces: dict[str | None, str] | None = ...,
602620
prefix: str | None = ...,
603621
encoding: str = ...,
@@ -1690,7 +1708,7 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
16901708
def columns(self) -> Index[str]: ...
16911709
@columns.setter # setter needs to be right next to getter; otherwise mypy complains
16921710
def columns(
1693-
self, cols: AnyArrayLike | list[Hashable] | tuple[Hashable, ...]
1711+
self, cols: AnyArrayLike | SequenceNotStr[Hashable] | tuple[Hashable, ...]
16941712
) -> None: ...
16951713
@property
16961714
def dtypes(self) -> Series: ...
@@ -2368,8 +2386,8 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
23682386
def to_string(
23692387
self,
23702388
buf: FilePath | WriteBuffer[str],
2371-
columns: Sequence[Hashable] | Index | Series | None = ...,
2372-
col_space: int | list[int] | dict[Hashable, int] | None = ...,
2389+
columns: SequenceNotStr[Hashable] | Index | Series | None = ...,
2390+
col_space: int | list[int] | dict[HashableT, int] | None = ...,
23732391
header: _bool | list[_str] | tuple[str, ...] = ...,
23742392
index: _bool = ...,
23752393
na_rep: _str = ...,

tests/test_frame.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3820,6 +3820,37 @@ def _constructor(self) -> type[MyClass]:
38203820
check(assert_type(df[["a", "b"]], MyClass), MyClass)
38213821

38223822

3823+
def test_hashable_args() -> None:
3824+
# GH 1104
3825+
df = pd.DataFrame([["abc"]], columns=["test"], index=["ind"])
3826+
test = ["test"]
3827+
3828+
with ensure_clean() as path:
3829+
3830+
df.to_stata(path, version=117, convert_strl=test)
3831+
df.to_stata(path, version=117, convert_strl=["test"])
3832+
3833+
df.to_html(path, columns=test)
3834+
df.to_html(path, columns=["test"])
3835+
3836+
df.to_xml(path, attr_cols=test)
3837+
df.to_xml(path, attr_cols=["test"])
3838+
3839+
df.to_xml(path, elem_cols=test)
3840+
df.to_xml(path, elem_cols=["test"])
3841+
3842+
# Next lines should work, but it is a mypy bug
3843+
# https://github.com/python/mypy/issues/3004
3844+
# pyright accepts this, so we only type check for pyright,
3845+
# and also test the code with pytest
3846+
df.columns = test # type: ignore[assignment]
3847+
df.columns = ["test"] # type: ignore[assignment]
3848+
3849+
testDict = {"test": 1}
3850+
df.to_string("test", col_space=testDict)
3851+
df.to_string("test", col_space={"test": 1})
3852+
3853+
38233854
# GH 906
38243855
@pd.api.extensions.register_dataframe_accessor("geo")
38253856
class GeoAccessor: ...

0 commit comments

Comments
 (0)