Skip to content

CLN remove 'import pandas as pd' from pandas/core/generic.py #39152

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
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
16 changes: 9 additions & 7 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
from pandas.core.dtypes.inference import is_hashable
from pandas.core.dtypes.missing import isna, notna

import pandas as pd
from pandas.core import arraylike, indexing, missing, nanops
import pandas.core.algorithms as algos
from pandas.core.arrays import ExtensionArray
Expand All @@ -106,6 +105,7 @@
from pandas.core.internals import ArrayManager, BlockManager
from pandas.core.missing import find_valid_index
from pandas.core.ops import align_method_FRAME
from pandas.core.reshape.concat import concat
from pandas.core.shared_docs import _shared_docs
from pandas.core.sorting import get_indexer_indexer
from pandas.core.window import Expanding, ExponentialMovingWindow, Rolling, Window
Expand Down Expand Up @@ -5304,7 +5304,11 @@ def sample(
"when sampling from a Series."
)

weights = pd.Series(weights, dtype="float64")
if isinstance(self, ABCSeries):
func = self._constructor
else:
func = self._constructor_sliced
Comment on lines +5307 to +5310
Copy link
Member Author

Choose a reason for hiding this comment

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

Or is it better to import Series within this function so as to avoid an if-else block?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah you can just import Series here

Copy link
Contributor

Choose a reason for hiding this comment

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

the only reason you woul want this is to handle a subclass

Copy link
Contributor

Choose a reason for hiding this comment

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

but i guess this is ok

weights = func(weights, dtype="float64")

if len(weights) != axis_length:
raise ValueError(
Expand Down Expand Up @@ -5890,7 +5894,7 @@ def astype(
return self.copy()

# GH 19920: retain column metadata after concat
result = pd.concat(results, axis=1, copy=False)
result = concat(results, axis=1, copy=False)
result.columns = self.columns
return result

Expand Down Expand Up @@ -6254,7 +6258,7 @@ def convert_dtypes(
)
for col_name, col in self.items()
]
result = pd.concat(results, axis=1, copy=False)
result = concat(results, axis=1, copy=False)
return result

# ----------------------------------------------------------------------
Expand Down Expand Up @@ -6532,10 +6536,8 @@ def replace(

if isinstance(to_replace, (tuple, list)):
if isinstance(self, ABCDataFrame):
from pandas import Series

return self.apply(
Series._replace_single,
self._constructor_sliced._replace_single,
args=(to_replace, method, inplace, limit),
)
self = cast("Series", self)
Expand Down