Skip to content

resolving issue 1162 I change Hashable to HashableT in _LocIndexerFrame and it'… #1165

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 3 commits into from
Mar 12, 2025

Conversation

shirzady1934
Copy link
Contributor

@shirzady1934 shirzady1934 commented Mar 12, 2025

This PR resolves pandas-dev/pandas-stubs#1162 by replacing Hashable with HashableT in _LocIndexerFrame. I also verified the changes using mypy:

mypy test.py
Success: no issues found in 1 source file
  • Closes #xxxx (Replace xxxx with the Github issue number)
  • Tests added: Please use assert_type() to assert the type of any return value

Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

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

can you please add a test based on the example in #1162 ?

@shirzady1934
Copy link
Contributor Author

shirzady1934 commented Mar 12, 2025

This PR addresses the issue in #1162, where DataFrame.loc[] should accept a list[str] for indexing.

To validate this behavior, I have written a test that:

import pandas as pd
import pytest
def test_loc_with_list_of_strings():
    df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
                      index=['cobra', 'viper', 'sidewinder'],
                      columns=['max_speed', 'shield'])
    
    result = df.loc[['viper', 'sidewinder']]

    assert assert_type(result, pd.DataFrame)
    assert list(result.index) == ['viper', 'sidewinder']
    assert result.shape == (2, 2)  # Expected shape: 2 rows, 2 columns
    

# Run the test
pytest.main()

Test Details:

Asserts that the result is a DataFrame.
Confirms the resulting DataFrame has the expected index ['viper', 'sidewinder'].
Validates the shape is (2, 2) (2 rows, 2 columns).
Checks specific values for rows 'viper' and 'sidewinder'.

The test passed successfully, confirming that DataFrame.loc[] behaves as expected with a list of strings.

result:

collected 1134 items                                                                                                                    

pandas-stubs/tests/test_api_types.py ....................................                                                         [  3%]
pandas-stubs/tests/test_api_typing.py ....s.............                                                                          [  4%]
pandas-stubs/tests/test_config.py ...                                                                                             [  5%]
pandas-stubs/tests/test_dtypes.py ..........                                                                                      [  5%]
pandas-stubs/tests/test_errors.py .........................s..........                                                            [  9%]
pandas-stubs/tests/test_extension.py ...                                                                                          [  9%]
pandas-stubs/tests/test_frame.py ................................................................................................ [ 17%]
.....................s.........................................................................................................   [ 29%]
pandas-stubs/tests/test_groupby.py ...........                                                                                    [ 29%]
pandas-stubs/tests/test_holidays.py .                                                                                             [ 30%]
pandas-stubs/tests/test_indexes.py .............................................                                                  [ 34%]
pandas-stubs/tests/test_interval.py .....                                                                                         [ 34%]
pandas-stubs/tests/test_interval_index.py ......                                                                                  [ 35%]
pandas-stubs/tests/test_io.py ....................sssss....................................s............                          [ 41%]
pandas-stubs/tests/test_merge.py .                                                                                                [ 41%]
pandas-stubs/tests/test_pandas.py .............................                                                                   [ 44%]
pandas-stubs/tests/test_plotting.py .............................                                                                 [ 46%]
pandas-stubs/tests/test_resampler.py ................................                                                             [ 49%]
pandas-stubs/tests/test_scalars.py ..............................                                                                 [ 52%]
pandas-stubs/tests/test_series.py ............................................................................................... [ 60%]
............................................................sss.................................................................. [ 71%]
................................................................................................................................. [ 83%]
................................................................                                                                  [ 88%]
pandas-stubs/tests/test_string_accessors.py ....................                                                                  [ 90%]
pandas-stubs/tests/test_styler.py ...........................                                                                     [ 93%]
pandas-stubs/tests/test_testing.py ...                                                                                            [ 93%]
pandas-stubs/tests/test_timefuncs.py ...................................................                                          [ 97%]
pandas-stubs/tests/test_utility.py ....                                                                                           [ 98%]
pandas-stubs/tests/test_windowing.py ....................                                                                         [100%]

=================================================== 1122 passed, 12 skipped in 22.19s ===================================================

is that okay?

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Mar 12, 2025

is that okay?

Your test should follow the pattern (and be included) in tests/test_frame.py, using check and assert_type and be part of this PR.

See the function test_in_columns() in that file as an example, and create your own function with a reference to #1162 in a comment.

@shirzady1934
Copy link
Contributor Author

I’ve added the test_loc_list_str function to verify that DataFrame.loc[] works as expected when using a list of strings for indexing. Please let me know if this works for you!

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Mar 12, 2025

precommit is failing because you didn't format with black

See https://github.com/pandas-dev/pandas-stubs/blob/main/docs/setup.md to set up your environment.

@shirzady1934
Copy link
Contributor Author

I've installed precommit and made the commit again.

Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

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

thanks @shirzady1934

@Dr-Irv Dr-Irv merged commit f6f7bb0 into pandas-dev:main Mar 12, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DataFrame.loc[] accepts list[str]
2 participants