Skip to content

CLN, TYP use from __future__ import annotations #39036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3220,7 +3220,7 @@ def _setitem_frame(self, key, value):
self._check_setitem_copy()
self._where(-key, value, inplace=True)

def _set_item_frame_value(self, key, value: "DataFrame") -> None:
def _set_item_frame_value(self, key, value: DataFrame) -> None:
self._ensure_valid_index(value)

# align right-hand-side columns if self.columns
Expand Down
8 changes: 5 additions & 3 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from contextlib import suppress
from typing import TYPE_CHECKING, Any, Hashable, List, Sequence, Tuple, Union
import warnings
Expand Down Expand Up @@ -1742,7 +1744,7 @@ def _setitem_with_indexer_2d_value(self, indexer, value):
# setting with a list, re-coerces
self._setitem_single_column(loc, value[:, i].tolist(), pi)

def _setitem_with_indexer_frame_value(self, indexer, value: "DataFrame", name: str):
def _setitem_with_indexer_frame_value(self, indexer, value: DataFrame, name: str):
ilocs = self._ensure_iterable_column_indexer(indexer[1])

sub_indexer = list(indexer)
Expand Down Expand Up @@ -2032,7 +2034,7 @@ def ravel(i):

raise ValueError("Incompatible indexer with Series")

def _align_frame(self, indexer, df: "DataFrame"):
def _align_frame(self, indexer, df: DataFrame):
is_frame = self.ndim == 2

if isinstance(indexer, tuple):
Expand Down Expand Up @@ -2204,7 +2206,7 @@ def _tuplify(ndim: int, loc: Hashable) -> Tuple[Union[Hashable, slice], ...]:
return tuple(_tup)


def convert_to_index_sliceable(obj: "DataFrame", key):
def convert_to_index_sliceable(obj: DataFrame, key):
"""
If we are index sliceable, then return my slicer, otherwise return None.
"""
Expand Down
8 changes: 5 additions & 3 deletions pandas/core/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

This is not a public API.
"""
from __future__ import annotations

import operator
from typing import TYPE_CHECKING, Optional, Set
import warnings
Expand Down Expand Up @@ -293,7 +295,7 @@ def to_series(right):


def should_reindex_frame_op(
left: "DataFrame", right, op, axis, default_axis, fill_value, level
left: DataFrame, right, op, axis, default_axis, fill_value, level
) -> bool:
"""
Check if this is an operation between DataFrames that will need to reindex.
Expand Down Expand Up @@ -322,7 +324,7 @@ def should_reindex_frame_op(


def frame_arith_method_with_reindex(
left: "DataFrame", right: "DataFrame", op
left: DataFrame, right: DataFrame, op
) -> "DataFrame":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here

"""
For DataFrame-with-DataFrame operations that require reindexing,
Expand Down Expand Up @@ -367,7 +369,7 @@ def frame_arith_method_with_reindex(
return result


def _maybe_align_series_as_frame(frame: "DataFrame", series: "Series", axis: int):
def _maybe_align_series_as_frame(frame: DataFrame, series: "Series", axis: int):
"""
If the Series operand is not EA-dtype, we can broadcast to 2D and operate
blockwise.
Expand Down
8 changes: 5 additions & 3 deletions pandas/core/reshape/melt.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import re
from typing import TYPE_CHECKING, List, cast
import warnings
Expand All @@ -24,7 +26,7 @@

@Appender(_shared_docs["melt"] % {"caller": "pd.melt(df, ", "other": "DataFrame.melt"})
def melt(
frame: "DataFrame",
frame: DataFrame,
id_vars=None,
value_vars=None,
var_name=None,
Expand Down Expand Up @@ -139,7 +141,7 @@ def melt(


@deprecate_kwarg(old_arg_name="label", new_arg_name=None)
def lreshape(data: "DataFrame", groups, dropna: bool = True, label=None) -> "DataFrame":
def lreshape(data: DataFrame, groups, dropna: bool = True, label=None) -> "DataFrame":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here

"""
Reshape wide-format data to long. Generalized inverse of DataFrame.pivot.

Expand Down Expand Up @@ -234,7 +236,7 @@ def lreshape(data: "DataFrame", groups, dropna: bool = True, label=None) -> "Dat


def wide_to_long(
df: "DataFrame", stubnames, i, j, sep: str = "", suffix: str = r"\d+"
df: DataFrame, stubnames, i, j, sep: str = "", suffix: str = r"\d+"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here

) -> "DataFrame":
r"""
Wide panel to long format. Less flexible but more user-friendly than melt.
Expand Down
25 changes: 13 additions & 12 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
SQL-style merge routines
"""
from __future__ import annotations

import copy
import datetime
Expand Down Expand Up @@ -99,7 +100,7 @@ def merge(
merge.__doc__ = _merge_doc % "\nleft : DataFrame"


def _groupby_and_merge(by, on, left: "DataFrame", right: "DataFrame", merge_pieces):
def _groupby_and_merge(by, on, left: DataFrame, right: DataFrame, merge_pieces):
"""
groupby & merge; we are always performing a left-by type operation

Expand Down Expand Up @@ -157,8 +158,8 @@ def _groupby_and_merge(by, on, left: "DataFrame", right: "DataFrame", merge_piec


def merge_ordered(
left: "DataFrame",
right: "DataFrame",
left: DataFrame,
right: DataFrame,
on: Optional[IndexLabel] = None,
left_on: Optional[IndexLabel] = None,
right_on: Optional[IndexLabel] = None,
Expand Down Expand Up @@ -300,8 +301,8 @@ def _merger(x, y):


def merge_asof(
left: "DataFrame",
right: "DataFrame",
left: DataFrame,
right: DataFrame,
on: Optional[IndexLabel] = None,
left_on: Optional[IndexLabel] = None,
right_on: Optional[IndexLabel] = None,
Expand Down Expand Up @@ -717,12 +718,12 @@ def get_result(self):

return result.__finalize__(self, method="merge")

def _maybe_drop_cross_column(self, result: "DataFrame", cross_col: Optional[str]):
def _maybe_drop_cross_column(self, result: DataFrame, cross_col: Optional[str]):
if cross_col is not None:
result.drop(columns=cross_col, inplace=True)

def _indicator_pre_merge(
self, left: "DataFrame", right: "DataFrame"
self, left: DataFrame, right: DataFrame
) -> Tuple["DataFrame", "DataFrame"]:

columns = left.columns.union(right.columns)
Expand Down Expand Up @@ -1230,7 +1231,7 @@ def _maybe_coerce_merge_keys(self):
self.right = self.right.assign(**{name: self.right[name].astype(typ)})

def _create_cross_configuration(
self, left: "DataFrame", right: "DataFrame"
self, left: DataFrame, right: DataFrame
) -> Tuple["DataFrame", "DataFrame", str, str]:
"""
Creates the configuration to dispatch the cross operation to inner join,
Expand Down Expand Up @@ -1546,8 +1547,8 @@ class _OrderedMerge(_MergeOperation):

def __init__(
self,
left: "DataFrame",
right: "DataFrame",
left: DataFrame,
right: DataFrame,
on: Optional[IndexLabel] = None,
left_on: Optional[IndexLabel] = None,
right_on: Optional[IndexLabel] = None,
Expand Down Expand Up @@ -1640,8 +1641,8 @@ class _AsOfMerge(_OrderedMerge):

def __init__(
self,
left: "DataFrame",
right: "DataFrame",
left: DataFrame,
right: DataFrame,
on: Optional[IndexLabel] = None,
left_on: Optional[IndexLabel] = None,
right_on: Optional[IndexLabel] = None,
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import (
TYPE_CHECKING,
Callable,
Expand Down Expand Up @@ -367,7 +369,7 @@ def _all_key(key):


def _generate_marginal_results_without_values(
table: "DataFrame", data, rows, cols, aggfunc, observed, margins_name: str = "All"
table: DataFrame, data, rows, cols, aggfunc, observed, margins_name: str = "All"
):
if len(cols) > 0:
# need to "interleave" the margins
Expand Down Expand Up @@ -421,7 +423,7 @@ def _convert_by(by):
@Substitution("\ndata : DataFrame")
@Appender(_shared_docs["pivot"], indents=1)
def pivot(
data: "DataFrame",
data: DataFrame,
index: Optional[Union[Label, Sequence[Label]]] = None,
columns: Optional[Union[Label, Sequence[Label]]] = None,
values: Optional[Union[Label, Sequence[Label]]] = None,
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Provide a generic structure to support window functions,
similar to how we have a Groupby object.
"""
from __future__ import annotations

from datetime import timedelta
from functools import partial
import inspect
Expand Down Expand Up @@ -314,7 +316,7 @@ def _prep_values(self, values: Optional[np.ndarray] = None) -> np.ndarray:

return values

def _insert_on_column(self, result: "DataFrame", obj: "DataFrame"):
def _insert_on_column(self, result: DataFrame, obj: DataFrame):
# if we have an 'on' column we want to put it back into
# the results in the same location
from pandas import Series
Expand Down
7 changes: 4 additions & 3 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Internal module for formatting output data in csv, html,
and latex files. This module also applies to display formatting.
"""
from __future__ import annotations

from contextlib import contextmanager
from csv import QUOTE_NONE, QUOTE_NONNUMERIC
Expand Down Expand Up @@ -462,7 +463,7 @@ class DataFrameFormatter:

def __init__(
self,
frame: "DataFrame",
frame: DataFrame,
columns: Optional[Sequence[str]] = None,
col_space: Optional[ColspaceArgType] = None,
header: Union[bool, Sequence[str]] = True,
Expand Down Expand Up @@ -813,7 +814,7 @@ def _get_formatter(self, i: Union[str, int]) -> Optional[Callable]:
i = self.columns[i]
return self.formatters.get(i, None)

def _get_formatted_column_labels(self, frame: "DataFrame") -> List[List[str]]:
def _get_formatted_column_labels(self, frame: DataFrame) -> List[List[str]]:
from pandas.core.indexes.multi import sparsify_labels

columns = frame.columns
Expand Down Expand Up @@ -854,7 +855,7 @@ def space_format(x, y):
# self.str_columns = str_columns
return str_columns

def _get_formatted_index(self, frame: "DataFrame") -> List[str]:
def _get_formatted_index(self, frame: DataFrame) -> List[str]:
# Note: this is only used by to_string() and to_latex(), not by
# to_html(). so safe to cast col_space here.
col_space = {k: cast(int, v) for k, v in self.col_space.items()}
Expand Down
8 changes: 5 additions & 3 deletions pandas/io/formats/info.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from abc import ABC, abstractmethod
import sys
from typing import (
Expand Down Expand Up @@ -227,10 +229,10 @@ class DataFrameInfo(BaseInfo):

def __init__(
self,
data: "DataFrame",
data: DataFrame,
memory_usage: Optional[Union[bool, str]] = None,
):
self.data: "DataFrame" = data
self.data: DataFrame = data
self.memory_usage = _initialize_memory_usage(memory_usage)

@property
Expand Down Expand Up @@ -679,7 +681,7 @@ def _gen_columns(self) -> Iterator[str]:
yield pprint_thing(col)


def _get_dataframe_dtype_counts(df: "DataFrame") -> Mapping[str, int]:
def _get_dataframe_dtype_counts(df: DataFrame) -> Mapping[str, int]:
"""
Create mapping between datatypes and their number of occurences.
"""
Expand Down
4 changes: 3 additions & 1 deletion pandas/io/gbq.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
""" Google BigQuery support """
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union

from pandas.compat._optional import import_optional_dependency
Expand Down Expand Up @@ -195,7 +197,7 @@ def read_gbq(


def to_gbq(
dataframe: "DataFrame",
dataframe: DataFrame,
destination_table: str,
project_id: Optional[str] = None,
chunksize: Optional[int] = None,
Expand Down
4 changes: 3 additions & 1 deletion pandas/plotting/_core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import importlib
from typing import TYPE_CHECKING, Optional, Sequence, Tuple, Union

Expand Down Expand Up @@ -99,7 +101,7 @@ def hist_series(


def hist_frame(
data: "DataFrame",
data: DataFrame,
column: Union[Label, Sequence[Label]] = None,
by=None,
grid: bool = True,
Expand Down
10 changes: 6 additions & 4 deletions pandas/plotting/_matplotlib/misc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import random
from typing import TYPE_CHECKING, Dict, List, Optional, Set

Expand All @@ -21,7 +23,7 @@


def scatter_matrix(
frame: "DataFrame",
frame: DataFrame,
alpha=0.5,
figsize=None,
ax=None,
Expand Down Expand Up @@ -124,7 +126,7 @@ def _get_marker_compat(marker):


def radviz(
frame: "DataFrame",
frame: DataFrame,
class_column,
ax: Optional["Axes"] = None,
color=None,
Expand Down Expand Up @@ -212,7 +214,7 @@ def normalize(series):


def andrews_curves(
frame: "DataFrame",
frame: DataFrame,
class_column,
ax: Optional["Axes"] = None,
samples: int = 200,
Expand Down Expand Up @@ -334,7 +336,7 @@ def bootstrap_plot(


def parallel_coordinates(
frame: "DataFrame",
frame: DataFrame,
class_column,
cols=None,
ax: Optional["Axes"] = None,
Expand Down