Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions pandas-stubs/_libs/lib.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
from enum import Enum

no_default = None

class NoDefault(Enum):
no_default: int

def infer_dtype(value: object, skipna: bool = ...) -> str: ...
1 change: 1 addition & 0 deletions pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ JsonSeriesOrient = Literal["split", "records", "index"]
TimestampConvention = Literal["start", "end", "s", "e"]

CSVEngine = Literal["c", "python", "pyarrow", "python-fwf"]
CSVQuoting = Literal[0, 1, 2, 3]

HDFCompLib = Literal["zlib", "lzo", "bzip2", "blosc"]
ParquetEngine = Literal["auto", "pyarrow", "fastparquet"]
Expand Down
7 changes: 4 additions & 3 deletions pandas-stubs/io/clipboards.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ from pandas.core.series import Series
from pandas._typing import (
CompressionOptions,
CSVEngine,
CSVQuoting,
DtypeArg,
StorageOptions,
npt,
Expand Down Expand Up @@ -71,7 +72,7 @@ def read_clipboard(
decimal: str = ...,
lineterminator: str | None = ...,
quotechar: str = ...,
quoting: int = ...,
quoting: CSVQuoting | None = ...,
doublequote: bool = ...,
escapechar: str | None = ...,
comment: str | None = ...,
Expand Down Expand Up @@ -137,7 +138,7 @@ def read_clipboard(
decimal: str = ...,
lineterminator: str | None = ...,
quotechar: str = ...,
quoting: int = ...,
quoting: CSVQuoting | None = ...,
doublequote: bool = ...,
escapechar: str | None = ...,
comment: str | None = ...,
Expand Down Expand Up @@ -203,7 +204,7 @@ def read_clipboard(
decimal: str = ...,
lineterminator: str | None = ...,
quotechar: str = ...,
quoting: int = ...,
quoting: CSVQuoting | None = ...,
doublequote: bool = ...,
escapechar: str | None = ...,
comment: str | None = ...,
Expand Down
35 changes: 0 additions & 35 deletions pandas-stubs/io/parsers/base_parser.pyi

This file was deleted.

49 changes: 25 additions & 24 deletions pandas-stubs/io/parsers/readers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import pandas._libs.lib as lib
from pandas._typing import (
CompressionOptions,
CSVEngine,
CSVQuoting,
DtypeArg,
FilePath,
ReadCsvBuffer,
Expand All @@ -30,10 +31,10 @@ from pandas.io.common import IOHandles
def read_csv(
filepath_or_buffer: FilePath | ReadCsvBuffer[bytes] | ReadCsvBuffer[str],
*,
sep: str | None | lib.NoDefault = ...,
delimiter: str | None | lib.NoDefault = ...,
sep: str | None = ...,
delimiter: str | None = ...,
header: int | Sequence[int] | Literal["infer"] | None = ...,
names: list[str] | None | lib.NoDefault = ...,
names: list[str] | None = ...,
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
usecols: list[str]
| tuple[str, ...]
Expand Down Expand Up @@ -74,7 +75,7 @@ def read_csv(
decimal: str = ...,
lineterminator: str | None = ...,
quotechar: str = ...,
quoting: int = ...,
quoting: CSVQuoting | None = ...,
doublequote: bool = ...,
escapechar: str | None = ...,
comment: str | None = ...,
Expand All @@ -93,10 +94,10 @@ def read_csv(
def read_csv(
filepath_or_buffer: FilePath | ReadCsvBuffer[bytes] | ReadCsvBuffer[str],
*,
sep: str | None | lib.NoDefault = ...,
delimiter: str | None | lib.NoDefault = ...,
sep: str | None = ...,
delimiter: str | None = ...,
header: int | Sequence[int] | Literal["infer"] | None = ...,
names: list[str] | None | lib.NoDefault = ...,
names: list[str] | None = ...,
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
usecols: list[str]
| tuple[str, ...]
Expand Down Expand Up @@ -137,7 +138,7 @@ def read_csv(
decimal: str = ...,
lineterminator: str | None = ...,
quotechar: str = ...,
quoting: int = ...,
quoting: CSVQuoting | None = ...,
doublequote: bool = ...,
escapechar: str | None = ...,
comment: str | None = ...,
Expand All @@ -156,10 +157,10 @@ def read_csv(
def read_csv(
filepath_or_buffer: FilePath | ReadCsvBuffer[bytes] | ReadCsvBuffer[str],
*,
sep: str | None | lib.NoDefault = ...,
delimiter: str | None | lib.NoDefault = ...,
sep: str | None = ...,
delimiter: str | None = ...,
header: int | Sequence[int] | Literal["infer"] | None = ...,
names: list[str] | None | lib.NoDefault = ...,
names: list[str] | None = ...,
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
usecols: list[str]
| tuple[str, ...]
Expand Down Expand Up @@ -200,7 +201,7 @@ def read_csv(
decimal: str = ...,
lineterminator: str | None = ...,
quotechar: str = ...,
quoting: int = ...,
quoting: CSVQuoting | None = ...,
doublequote: bool = ...,
escapechar: str | None = ...,
comment: str | None = ...,
Expand All @@ -219,10 +220,10 @@ def read_csv(
def read_table(
filepath_or_buffer: FilePath | ReadCsvBuffer[bytes] | ReadCsvBuffer[str],
*,
sep: str | None | lib.NoDefault = ...,
delimiter: str | None | lib.NoDefault = ...,
sep: str | None = ...,
delimiter: str | None = ...,
header: int | Sequence[int] | Literal["infer"] | None = ...,
names: list[str] | None | lib.NoDefault = ...,
names: list[str] | None = ...,
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
usecols: list[str]
| tuple[str, ...]
Expand Down Expand Up @@ -263,7 +264,7 @@ def read_table(
decimal: str = ...,
lineterminator: str | None = ...,
quotechar: str = ...,
quoting: int = ...,
quoting: CSVQuoting | None = ...,
doublequote: bool = ...,
escapechar: str | None = ...,
comment: str | None = ...,
Expand All @@ -282,10 +283,10 @@ def read_table(
def read_table(
filepath_or_buffer: FilePath | ReadCsvBuffer[bytes] | ReadCsvBuffer[str],
*,
sep: str | None | lib.NoDefault = ...,
delimiter: str | None | lib.NoDefault = ...,
sep: str | None = ...,
delimiter: str | None = ...,
header: int | Sequence[int] | Literal["infer"] | None = ...,
names: list[str] | None | lib.NoDefault = ...,
names: list[str] | None = ...,
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
usecols: list[str]
| tuple[str, ...]
Expand Down Expand Up @@ -326,7 +327,7 @@ def read_table(
decimal: str = ...,
lineterminator: str | None = ...,
quotechar: str = ...,
quoting: int = ...,
quoting: CSVQuoting | None = ...,
doublequote: bool = ...,
escapechar: str | None = ...,
comment: str | None = ...,
Expand All @@ -345,10 +346,10 @@ def read_table(
def read_table(
filepath_or_buffer: FilePath | ReadCsvBuffer[bytes] | ReadCsvBuffer[str],
*,
sep: str | None | lib.NoDefault = ...,
delimiter: str | None | lib.NoDefault = ...,
sep: str | None = ...,
delimiter: str | None = ...,
header: int | Sequence[int] | Literal["infer"] | None = ...,
names: list[str] | None | lib.NoDefault = ...,
names: list[str] | None = ...,
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
usecols: list[str]
| tuple[str, ...]
Expand Down Expand Up @@ -389,7 +390,7 @@ def read_table(
decimal: str = ...,
lineterminator: str | None = ...,
quotechar: str = ...,
quoting: int = ...,
quoting: CSVQuoting | None = ...,
doublequote: bool = ...,
escapechar: str | None = ...,
comment: str | None = ...,
Expand Down
112 changes: 112 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Union,
)

import numpy as np
from packaging.version import parse
import pandas as pd
from pandas import (
Expand Down Expand Up @@ -381,6 +382,48 @@ def test_read_csv_iterator():
)


def test_types_read_csv() -> None:
df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]})
csv_df: str = df.to_csv()

with ensure_clean() as path:
df.to_csv(path)
df2: pd.DataFrame = pd.read_csv(path)
df3: pd.DataFrame = pd.read_csv(path, sep="a")
df4: pd.DataFrame = pd.read_csv(
path,
header=None,
)
df5: pd.DataFrame = pd.read_csv(
path, engine="python", true_values=["no", "No", "NO"], na_filter=False
)
df6: pd.DataFrame = pd.read_csv(
path,
skiprows=lambda x: x in [0, 2],
skip_blank_lines=True,
dayfirst=False,
)
df7: pd.DataFrame = pd.read_csv(path, nrows=2)
df8: pd.DataFrame = pd.read_csv(path, dtype={"a": float, "b": int})
df9: pd.DataFrame = pd.read_csv(path, usecols=["col1"])
df10: pd.DataFrame = pd.read_csv(path, usecols=[0])
df11: pd.DataFrame = pd.read_csv(path, usecols=np.array([0]))
df12: pd.DataFrame = pd.read_csv(path, usecols=("col1",))
df13: pd.DataFrame = pd.read_csv(path, usecols=pd.Series(data=["col1"]))

tfr1: TextFileReader = pd.read_csv(path, nrows=2, iterator=True, chunksize=3)
tfr1.close()

tfr2: TextFileReader = pd.read_csv(path, nrows=2, chunksize=1)
tfr2.close()

tfr3: TextFileReader = pd.read_csv(path, nrows=2, iterator=False, chunksize=1)
tfr3.close()

tfr4: TextFileReader = pd.read_csv(path, nrows=2, iterator=True)
tfr4.close()


def test_read_table():
with ensure_clean() as path:
check(assert_type(DF.to_csv(path, sep="\t"), None), type(None))
Expand Down Expand Up @@ -455,6 +498,75 @@ def test_to_csv_series():
check(assert_type(s.to_csv(path), None), type(None))


def test_read_sql():
with ensure_clean() as path:
con = sqlite3.connect(path)
check(assert_type(DF.to_sql("test", con=con), Union[int, None]), int)
check(
assert_type(read_sql("select * from test", con=con), DataFrame), DataFrame
)
con.close()


def test_read_sql_generator():
with ensure_clean() as path:
con = sqlite3.connect(path)
check(assert_type(DF.to_sql("test", con=con), Union[int, None]), int)

check(
assert_type(
read_sql("select * from test", con=con, chunksize=1),
Generator[DataFrame, None, None],
),
Generator,
)
con.close()


def test_read_sql_table():
if TYPE_CHECKING:
# sqlite3 doesn't support read_table, which is required for this function
# Could only run in pytest if SQLAlchemy was installed
with ensure_clean() as path:
con = sqlite3.connect(path)
assert_type(DF.to_sql("test", con=con), Union[int, None])
assert_type(read_sql_table("test", con=con), DataFrame)
assert_type(
read_sql_table("test", con=con, chunksize=1),
Generator[DataFrame, None, None],
)
con.close()


def test_read_sql_query():
with ensure_clean() as path:
con = sqlite3.connect(path)
check(assert_type(DF.to_sql("test", con=con), Union[int, None]), int)
check(
assert_type(
read_sql_query("select * from test", con=con, index_col="index"),
DataFrame,
),
DataFrame,
)
con.close()


def test_read_sql_query_generator():
with ensure_clean() as path:
con = sqlite3.connect(path)
check(assert_type(DF.to_sql("test", con=con), Union[int, None]), int)

check(
assert_type(
read_sql_query("select * from test", con=con, chunksize=1),
Generator[DataFrame, None, None],
),
Generator,
)
con.close()


def test_read_html():
check(assert_type(DF.to_html(), str), str)
with ensure_clean() as path:
Expand Down
Loading