-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
CLN: pandas\io\formats\format.py #27577
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
jreback
merged 2 commits into
pandas-dev:master
from
simonjayhawkins:clean-pandas-io-formats-format.py
Jul 25, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,6 @@ | |
from dateutil.tz.tz import tzutc | ||
from dateutil.zoneinfo import tzfile | ||
import numpy as np | ||
from numpy import float64, int32, ndarray | ||
|
||
from pandas._config.config import get_option, set_option | ||
|
||
|
@@ -1124,7 +1123,7 @@ def __init__( | |
self.fixed_width = fixed_width | ||
self.leading_space = leading_space | ||
|
||
def get_result(self) -> Union[ndarray, List[str]]: | ||
def get_result(self) -> List[str]: | ||
fmt_values = self._format_strings() | ||
return _make_fixed_width(fmt_values, self.justify) | ||
|
||
|
@@ -1260,7 +1259,7 @@ def formatter(value): | |
|
||
return formatter | ||
|
||
def get_result_as_array(self) -> Union[ndarray, List[str]]: | ||
def get_result_as_array(self) -> np.ndarray: | ||
""" | ||
Returns the float values converted into strings using | ||
the parameters given at initialisation, as a numpy array | ||
|
@@ -1300,9 +1299,10 @@ def format_values_with(float_format): | |
|
||
if self.fixed_width: | ||
if is_complex: | ||
return _trim_zeros_complex(values, na_rep) | ||
result = _trim_zeros_complex(values, na_rep) | ||
else: | ||
return _trim_zeros_float(values, na_rep) | ||
result = _trim_zeros_float(values, na_rep) | ||
return np.asarray(result, dtype="object") | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return values | ||
|
||
|
@@ -1367,7 +1367,7 @@ def _format_strings(self) -> List[str]: | |
class Datetime64Formatter(GenericArrayFormatter): | ||
def __init__( | ||
self, | ||
values: Union[ndarray, "Series", DatetimeIndex, DatetimeArray], | ||
values: Union[np.ndarray, "Series", DatetimeIndex, DatetimeArray], | ||
nat_rep: str = "NaT", | ||
date_format: None = None, | ||
**kwargs | ||
|
@@ -1424,7 +1424,7 @@ def _format_strings(self) -> List[str]: | |
|
||
def format_percentiles( | ||
percentiles: Union[ | ||
ndarray, List[Union[int, float]], List[float], List[Union[str, float]] | ||
np.ndarray, List[Union[int, float]], List[float], List[Union[str, float]] | ||
] | ||
) -> List[str]: | ||
""" | ||
|
@@ -1492,7 +1492,9 @@ def format_percentiles( | |
return [i + "%" for i in out] | ||
|
||
|
||
def _is_dates_only(values: Union[ndarray, DatetimeArray, Index, DatetimeIndex]) -> bool: | ||
def _is_dates_only( | ||
values: Union[np.ndarray, DatetimeArray, Index, DatetimeIndex] | ||
) -> bool: | ||
# return a boolean if we are only dates (and don't have a timezone) | ||
assert values.ndim == 1 | ||
|
||
|
@@ -1556,7 +1558,7 @@ def _get_format_datetime64( | |
|
||
|
||
def _get_format_datetime64_from_values( | ||
values: Union[ndarray, DatetimeArray, DatetimeIndex], date_format: Optional[str] | ||
values: Union[np.ndarray, DatetimeArray, DatetimeIndex], date_format: Optional[str] | ||
) -> Optional[str]: | ||
""" given values and a date_format, return a string format """ | ||
|
||
|
@@ -1588,7 +1590,7 @@ def _format_strings(self) -> List[str]: | |
class Timedelta64Formatter(GenericArrayFormatter): | ||
def __init__( | ||
self, | ||
values: Union[ndarray, TimedeltaIndex], | ||
values: Union[np.ndarray, TimedeltaIndex], | ||
nat_rep: str = "NaT", | ||
box: bool = False, | ||
**kwargs | ||
|
@@ -1597,16 +1599,15 @@ def __init__( | |
self.nat_rep = nat_rep | ||
self.box = box | ||
|
||
def _format_strings(self) -> ndarray: | ||
def _format_strings(self) -> List[str]: | ||
formatter = self.formatter or _get_format_timedelta64( | ||
self.values, nat_rep=self.nat_rep, box=self.box | ||
) | ||
fmt_values = np.array([formatter(x) for x in self.values]) | ||
return fmt_values | ||
return [formatter(x) for x in self.values] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the most relevant change here. return type now consistent with parent class and other formatters. mypy should have picked this up. but since we don't have numpy stubs, all numpy types resolve to Any |
||
|
||
|
||
def _get_format_timedelta64( | ||
values: Union[ndarray, TimedeltaIndex, TimedeltaArray], | ||
values: Union[np.ndarray, TimedeltaIndex, TimedeltaArray], | ||
nat_rep: str = "NaT", | ||
box: bool = False, | ||
) -> Callable: | ||
|
@@ -1651,11 +1652,11 @@ def _formatter(x): | |
|
||
|
||
def _make_fixed_width( | ||
strings: Union[ndarray, List[str]], | ||
strings: List[str], | ||
justify: str = "right", | ||
minimum: Optional[int] = None, | ||
adj: Optional[TextAdjustment] = None, | ||
) -> Union[ndarray, List[str]]: | ||
) -> List[str]: | ||
|
||
if len(strings) == 0 or justify == "all": | ||
return strings | ||
|
@@ -1683,7 +1684,7 @@ def just(x): | |
return result | ||
|
||
|
||
def _trim_zeros_complex(str_complexes: ndarray, na_rep: str = "NaN") -> List[str]: | ||
def _trim_zeros_complex(str_complexes: np.ndarray, na_rep: str = "NaN") -> List[str]: | ||
""" | ||
Separates the real and imaginary parts from the complex number, and | ||
executes the _trim_zeros_float method on each of those. | ||
|
@@ -1702,7 +1703,7 @@ def separate_and_trim(str_complex, na_rep): | |
|
||
|
||
def _trim_zeros_float( | ||
str_floats: Union[ndarray, List[str]], na_rep: str = "NaN" | ||
str_floats: Union[np.ndarray, List[str]], na_rep: str = "NaN" | ||
) -> List[str]: | ||
""" | ||
Trims zeros, leaving just one before the decimal points if need be. | ||
|
@@ -1766,7 +1767,7 @@ def __init__(self, accuracy: Optional[int] = None, use_eng_prefix: bool = False) | |
self.accuracy = accuracy | ||
self.use_eng_prefix = use_eng_prefix | ||
|
||
def __call__(self, num: Union[float64, int, float]) -> str: | ||
def __call__(self, num: Union[int, float]) -> str: | ||
""" Formats a number in engineering notation, appending a letter | ||
representing the power of 1000 of the original number. Some examples: | ||
|
||
|
@@ -1846,7 +1847,7 @@ def set_eng_float_format(accuracy: int = 3, use_eng_prefix: bool = False) -> Non | |
set_option("display.column_space", max(12, accuracy + 9)) | ||
|
||
|
||
def _binify(cols: List[int32], line_width: Union[int32, int]) -> List[int]: | ||
def _binify(cols: List[np.int32], line_width: Union[np.int32, int]) -> List[int]: | ||
adjoin_width = 1 | ||
bins = [] | ||
curr_width = 0 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use asarray or pass copy=False
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure. was keeping the code as close to original as possible.