google / google/yapf

Inconsistent formatting in .loc[] pandas function calls

Open
#900 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
14k
Forks
904
PR merge metrics
No merged PRs in 30d

Description

Hi :) Thanks for the great tool. We have run into some inconsistency problems, especially when using the `.loc` function calls on pandas.DataFrames, using the `~` operator and referencing constants on objects.

For reference I'm on yapf 0.30.0, Windows 10, python 3.8.

Heres some code and four different formatting results depending on very small changes:

```python
import pandas as pd

MY_CONST = "constant"

class My:
CONST = MY_CONST

some_very_long_df_name_with_some_data = pd.DataFrame()

some_very_long_df_name_with_some_data = some_very_long_df_name_with_some_data.loc[~some_very_long_df_name_with_some_data
.SOME_COLUMN_WE_HAVE_DEFINED].groupby(
My.CONST
)
# ^ Flake8 reports E126

# If I replace My.CONST with the variable MY_CONST I get this:
some_very_long_df_name_with_some_data = some_very_long_df_name_with_some_data.loc[
~some_very_long_df_name_with_some_data.SOME_COLUMN_WE_HAVE_DEFINED].groupby(MY_CONST)

# If I remove the ~ operator I get this:
some_very_long_df_name_with_some_data = some_very_long_df_name_with_some_data.loc[
some_very_long_df_name_with_some_data.SOME_COLUMN_WE_HAVE_DEFINED].groupby(My.CONST)

# If .loc had been a regular function call instead of the __getitem__ this is what I get:
some_very_long_df_name_with_some_data = some_very_long_df_name_with_some_data.loc(
~some_very_long_df_name_with_some_data.SOME_COLUMN_WE_HAVE_DEFINED
).groupby(My.CONST)
```

Here's my `.style.yapf`
```
[style]
based_on_style = pep8

align_closing_bracket_with_visual_indent=True

allow_multiline_dictionary_keys=False

allow_multiline_lambdas=True

allow_split_before_default_or_named_assigns=True

allow_split_before_dict_value=True

arithmetic_precedence_indication=False

blank_lines_around_top_level_definition=2

blank_line_before_class_docstring=False

blank_line_before_module_docstring=False

blank_line_before_nested_class_or_def=True

coalesce_brackets=False

column_limit=125

continuation_align_style=SPACE

continuation_indent_width=4

dedent_closing_brackets=True

disable_ending_comma_heuristic=False

each_dict_entry_on_separate_line=True

i18n_comment=

i18n_function_call=

indent_blank_lines=False

indent_dictionary_value=False

indent_width=4

join_multiple_lines=True

no_spaces_around_selected_binary_operators=

spaces_around_default_or_named_assign=False

spaces_around_power_operator=False

spaces_before_comment=2

space_between_ending_comma_and_closing_bracket=True

split_all_comma_separated_values=False

split_all_top_level_comma_separated_values=False

split_arguments_when_comma_terminated=True

split_before_arithmetic_operator=True

split_before_bitwise_operator=True

split_before_closing_bracket=True

split_before_dict_set_generator=True

split_before_dot=True

split_before_expression_after_opening_paren=False

split_before_first_argument=False

split_before_logical_operator=True

split_before_named_assigns=True

split_complex_comprehension=False

split_penalty_after_opening_bracket=300

split_penalty_after_unary_operator=10000

split_penalty_arithmetic_operator=300

split_penalty_before_if_expr=0

split_penalty_bitwise_operator=300

split_penalty_comprehension=80

split_penalty_excess_character=7000

split_penalty_for_added_line_split=30

split_penalty_import_names=0

split_penalty_logical_operator=300

use_tabs=False

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.