Skip to content

WARN read_table with infer_datetime_format doesn't show FutureWarning #51017

Closed
@MarcoGorelli

Description

@MarcoGorelli

Running

import pandas as pd
import io

timestamp_format = '%Y-%d-%m %H:%M:%S'
date_index = pd.date_range(start='1900', end='2000')
dates_df = date_index.strftime(timestamp_format).to_frame(name='ts_col')
data = dates_df.to_csv()
df = pd.read_csv(
    io.StringIO(data),
    date_parser=lambda x: pd.to_datetime(x, format=timestamp_format),
    parse_dates=['ts_col'],
    infer_datetime_format=True,
    sep=',',
)

results in

t.py:34: UserWarning: The argument 'infer_datetime_format' is deprecated and will be removed in a future version. A strict version of it is now the default, see https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. You can safely remove this argument.
  df = pd.read_csv(

However,

df = pd.read_table(
    io.StringIO(data),
    date_parser=lambda x: pd.to_datetime(x, format=timestamp_format),
    parse_dates=['ts_col'],
    infer_datetime_format=True,
    sep=',',
)

shows no warning

Task is just to add a warning to this function

def read_table(
filepath_or_buffer: FilePath | ReadCsvBuffer[bytes] | ReadCsvBuffer[str],
*,
sep: str | None | lib.NoDefault = lib.no_default,
delimiter: str | None | lib.NoDefault = None,
# Column and Index Locations and Names
header: int | Sequence[int] | None | Literal["infer"] = "infer",
names: Sequence[Hashable] | None | lib.NoDefault = lib.no_default,
index_col: IndexLabel | Literal[False] | None = None,
usecols=None,
# General Parsing Configuration
dtype: DtypeArg | None = None,
engine: CSVEngine | None = None,
converters=None,
true_values=None,
false_values=None,
skipinitialspace: bool = False,
skiprows=None,
skipfooter: int = 0,
nrows: int | None = None,
# NA and Missing Data Handling
na_values=None,
keep_default_na: bool = True,
na_filter: bool = True,
verbose: bool = False,
skip_blank_lines: bool = True,
# Datetime Handling
parse_dates: bool | Sequence[Hashable] = False,
infer_datetime_format: bool | lib.NoDefault = lib.no_default,
keep_date_col: bool = False,
date_parser=None,
dayfirst: bool = False,
cache_dates: bool = True,
# Iteration
iterator: bool = False,
chunksize: int | None = None,
# Quoting, Compression, and File Format
compression: CompressionOptions = "infer",
thousands: str | None = None,
decimal: str = ".",
lineterminator: str | None = None,
quotechar: str = '"',
quoting: int = csv.QUOTE_MINIMAL,
doublequote: bool = True,
escapechar: str | None = None,
comment: str | None = None,
encoding: str | None = None,
encoding_errors: str | None = "strict",
dialect: str | csv.Dialect | None = None,
# Error Handling
on_bad_lines: str = "error",
# Internal
delim_whitespace: bool = False,
low_memory=_c_parser_defaults["low_memory"],
memory_map: bool = False,
float_precision: str | None = None,
storage_options: StorageOptions = None,
use_nullable_dtypes: bool | lib.NoDefault = lib.no_default,
) -> DataFrame | TextFileReader:

similarly to how is already done here:

if infer_datetime_format is not lib.no_default:
warnings.warn(
"The argument 'infer_datetime_format' is deprecated and will "
"be removed in a future version. "
"A strict version of it is now the default, see "
"https://pandas.pydata.org/pdeps/0004-consistent-to-datetime-parsing.html. "
"You can safely remove this argument.",
stacklevel=find_stack_level(),
)

Finally, you'll need to add a test: you can duplicate

def test_parse_dates_infer_datetime_format_warning(all_parsers):
# GH 49024
parser = all_parsers
data = "Date,test\n2012-01-01,1\n,2"
parser.read_csv_check_warnings(
UserWarning,
"The argument 'infer_datetime_format' is deprecated",
StringIO(data),
parse_dates=["Date"],
infer_datetime_format=True,
)

but for read_table (or parametrise over parser.read_csv_check_warnings and parser.read_table_check_warnings). Note that you'll need to add sep=','

Metadata

Metadata

Assignees

Labels

WarningsWarnings that appear or should be added to pandasgood first issue

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions