|
28 | 28 | Appender,
|
29 | 29 | deprecate_nonkeyword_arguments,
|
30 | 30 | )
|
| 31 | +from pandas.util._exceptions import find_stack_level |
31 | 32 | from pandas.util._validators import validate_bool_kwarg
|
32 | 33 |
|
33 | 34 | from pandas.core.dtypes.common import (
|
|
131 | 132 | parsing time and lower memory usage.
|
132 | 133 | squeeze : bool, default False
|
133 | 134 | If the parsed data only contains one column then return a Series.
|
| 135 | +
|
| 136 | + .. deprecated:: 1.4.0 |
| 137 | + Append ``.squeeze("columns")`` to the call to ``{func_name}`` to squeeze |
| 138 | + the data. |
134 | 139 | prefix : str, optional
|
135 | 140 | Prefix to add to column numbers when no header, e.g. 'X' for X0, X1, ...
|
136 | 141 | mangle_dupe_cols : bool, default True
|
|
439 | 444 | "low_memory",
|
440 | 445 | }
|
441 | 446 |
|
442 |
| -_deprecated_defaults: dict[str, Any] = {"error_bad_lines": None, "warn_bad_lines": None} |
| 447 | +_deprecated_defaults: dict[str, Any] = { |
| 448 | + "error_bad_lines": None, |
| 449 | + "warn_bad_lines": None, |
| 450 | + "squeeze": None, |
| 451 | +} |
443 | 452 |
|
444 | 453 |
|
445 | 454 | def validate_integer(name, val, min_val=0):
|
@@ -552,7 +561,7 @@ def read_csv(
|
552 | 561 | names=lib.no_default,
|
553 | 562 | index_col=None,
|
554 | 563 | usecols=None,
|
555 |
| - squeeze=False, |
| 564 | + squeeze=None, |
556 | 565 | prefix=lib.no_default,
|
557 | 566 | mangle_dupe_cols=True,
|
558 | 567 | # General Parsing Configuration
|
@@ -650,7 +659,7 @@ def read_table(
|
650 | 659 | names=lib.no_default,
|
651 | 660 | index_col=None,
|
652 | 661 | usecols=None,
|
653 |
| - squeeze=False, |
| 662 | + squeeze=None, |
654 | 663 | prefix=lib.no_default,
|
655 | 664 | mangle_dupe_cols=True,
|
656 | 665 | # General Parsing Configuration
|
@@ -867,11 +876,12 @@ def __init__(self, f, engine=None, **kwds):
|
867 | 876 |
|
868 | 877 | self.chunksize = options.pop("chunksize", None)
|
869 | 878 | self.nrows = options.pop("nrows", None)
|
870 |
| - self.squeeze = options.pop("squeeze", False) |
871 | 879 |
|
872 | 880 | self._check_file_or_buffer(f, engine)
|
873 | 881 | self.options, self.engine = self._clean_options(options, engine)
|
874 | 882 |
|
| 883 | + self.squeeze = self.options.pop("squeeze", False) |
| 884 | + |
875 | 885 | if "has_index_names" in kwds:
|
876 | 886 | self.options["has_index_names"] = kwds["has_index_names"]
|
877 | 887 |
|
@@ -1050,7 +1060,7 @@ def _clean_options(self, options, engine):
|
1050 | 1060 | f"The {arg} argument has been deprecated and will be "
|
1051 | 1061 | "removed in a future version.\n\n"
|
1052 | 1062 | )
|
1053 |
| - warnings.warn(msg, FutureWarning, stacklevel=7) |
| 1063 | + warnings.warn(msg, FutureWarning, stacklevel=find_stack_level()) |
1054 | 1064 | else:
|
1055 | 1065 | result[arg] = parser_default
|
1056 | 1066 |
|
@@ -1100,6 +1110,10 @@ def _clean_options(self, options, engine):
|
1100 | 1110 | result["na_values"] = na_values
|
1101 | 1111 | result["na_fvalues"] = na_fvalues
|
1102 | 1112 | result["skiprows"] = skiprows
|
| 1113 | + # Default for squeeze is none since we need to check |
| 1114 | + # if user sets it. We then set to False to preserve |
| 1115 | + # previous behavior. |
| 1116 | + result["squeeze"] = False if options["squeeze"] is None else options["squeeze"] |
1103 | 1117 |
|
1104 | 1118 | return result, engine
|
1105 | 1119 |
|
@@ -1149,7 +1163,7 @@ def read(self, nrows=None):
|
1149 | 1163 | self._currow += new_rows
|
1150 | 1164 |
|
1151 | 1165 | if self.squeeze and len(df.columns) == 1:
|
1152 |
| - return df[df.columns[0]].copy() |
| 1166 | + return df.squeeze("columns").copy() |
1153 | 1167 | return df
|
1154 | 1168 |
|
1155 | 1169 | def get_chunk(self, size=None):
|
|
0 commit comments