Skip to content

BUG: extra leading space in to_string when index=False #29670

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

Conversation

charlesdong1991
Copy link
Member

@charlesdong1991 charlesdong1991 commented Nov 17, 2019

I still see people posting issue for the change of behaviour in to_string(index=False) on pandas such as #28538 , so I think it might be worth it to reopen my previous stalled PR #25000 and see if could get it solved this time.

All the changes have been made based on the reviews in PR #25000 and for detailed summary and explanation, please check #25000 (comment)
Feel free to take a look and any review and comments are very welcomed!

@@ -1095,7 +1106,7 @@ def format_array(
space: Optional[Union[str, int]] = None,
justify: str = "right",
decimal: str = ".",
leading_space: Optional[bool] = None,
leading_space: bool = "compat",
Copy link
Member

Choose a reason for hiding this comment

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

this isn't a boolean? Can you run mypy on your changes

Copy link
Member Author

Choose a reason for hiding this comment

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

oh, yeah, didn't see your comment when I committed my latest change. I changed already

"inputs, expected",
[([" a", " b"], " a\n b"), ([".1", "1"], ".1\n 1"), (["10", "-10"], " 10\n-10")],
)
def test_to_string_index_false_corner_case(inputs, expected):
Copy link
Member

Choose a reason for hiding this comment

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

I think these cases should just be added as param's to your test method below test_format_remove_leading_space_series

Copy link
Member Author

Choose a reason for hiding this comment

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

sure, this was just some corner cases that was found during the previous PR. will move.

@@ -937,13 +943,18 @@ def to_latex(
def _format_col(self, i: int) -> List[str]:
frame = self.tr_frame
formatter = self._get_formatter(i)
if self.index:
leading_space = "compat"
Copy link
Member

Choose a reason for hiding this comment

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

Seems like there was already some discussion around this here https://github.com/pandas-dev/pandas/pull/25000/files#r252237505 -

Copy link
Member Author

Choose a reason for hiding this comment

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

emm, i think i addressed it already in this PR?

@@ -1110,7 +1121,7 @@ def format_array(
space
justify
decimal
leading_space : bool, optional
leading_space : bool, default is 'compat'
Whether the array should be formatted with a leading space.
Copy link
Member

Choose a reason for hiding this comment

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

same comment as previous here what is the intended type of this?

Copy link
Member Author

@charlesdong1991 charlesdong1991 Nov 17, 2019

Choose a reason for hiding this comment

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

yeah, I didn't notice this so just copy paste the change in my previous PR. my bad, changed.

@alimcmaster1
Copy link
Member

thanks for the pr @charlesdong1991 - some comments above. We can re-open the previous PR if helpful? #25000

@charlesdong1991
Copy link
Member Author

thanks for your comment, @alimcmaster1
I already have this PR open and there has been dramatic changes since back then, so I think it is fine just to leave the old PR as is. But if you think it's helpful, feel free to re-open the stalled PR, and I don't think I am able to do it.

@alimcmaster1 alimcmaster1 added the Output-Formatting __repr__ of pandas objects, to_string label Nov 17, 2019
@charlesdong1991
Copy link
Member Author

Finally the annotation error is gone, it's green now 😅

pls take a look and see if this PR is useful.

@charlesdong1991
Copy link
Member Author

charlesdong1991 commented Dec 28, 2019

Hello, @jreback
I would appreciate a lot if you would also like to take a look at this PR which was closed long ago, but since other people posted the issue again, I reopened it with this PR.

All comments in the last closed PR were addressed.

@simontorres
Copy link

So, this PR seems ok. Anyone with the right permissions to fix conflict an merge? I've been forced to keep using version 0.23.0

@jreback
Copy link
Contributor

jreback commented Jan 7, 2020

@simontorres there are quite a number of comments on this
it is not so simple

i find it hard to believe this blocking an update of pandas

@simontorres
Copy link

I was talking out ignorance. I'm sorry

@charlesdong1991
Copy link
Member Author

Hi, @simontorres i resolved the conflict, and as @jreback said, there are some comments in the PR, and pls feel free to take over if you could find a better solution for this, and then i could close this PR to make way for you!

@simontorres
Copy link

Thanks @charlesdong1991 and @jreback . I don't think I can find a better solution but I will see if I can help.

@charlesdong1991
Copy link
Member Author

charlesdong1991 commented Jan 7, 2020

I am sure you could help with the issue and pandas 👍 @simontorres

also @jreback feel free to close this PR if you find it hard to approach!

@alimcmaster1
Copy link
Member

Thanks @charlesdong1991 and @jreback . I don't think I can find a better solution but I will see if I can help.

@simontorres
Could you provide a repo of the issue you faced? That way we can ensure we are covering your use case with a test case in pandas.

@simontorres
Copy link

Introduction/Application

One example is this Imaging Spectrograph called GoodmanHTS, there are two elements that can move a camera and a dispersor, their angle are recorded in the image's header. There are certain named modes that I'm interested in that can be used define file names, for instance. So this is a shorter version of a longer table.

import pandas

columns = ['grating_freq', 'wavmode', 'camtarg', 'grttarg', 'ob_filter']
spec_mode = [['400', 'm1', '11.6', '5.8', 'None'],
             ['400', 'm2', '16.1', '7.5', 'GG455'],
             ['600', 'UV', '15.25', '7.0', 'None'],
             ['600', 'Blue', '17.0', '7.0', 'None'],
             ['600', 'Mid', '20.0', '10.0', 'GG385'],
             ['600', 'Red', '27.0', '12.0', 'GG495'],
             ['930', 'm1', '20.6', '10.3', 'None'],
             ['930', 'm2', '25.2', '12.6', 'None'],
             ['930', 'm3', '29.9', '15.0', 'GG385'],
             ['930', 'm4', '34.6', '18.3', 'GG495'],
             ['930', 'm5', '39.4', '19.7', 'GG495']]

modes_data_frame = pandas.DataFrame(spec_mode, columns=columns)

The problem

The information I get is the Grating Frequency (for instance 930), the Camera Target Angle (camtarg, 20.6), the Grating Target Angle (grttarg, 10.3) and the Order Blocking Filter (. When I use pandas to find the match in the following way.

mode = modes_data_frame[
    ((modes_data_frame['grating_freq'] == 930) &
     (modes_data_frame['camtarg'] == 20.6) &
     (modes_data_frame['grttarg'] == 10.3) &
     (modes_data_frame['ob_filter'] == None))]

if mode.empty:
    central_wavelength = get_central_wavelength(*some_args)
    return 'Custom_{:d}nm'.format(central_wavelength)
else:
    return mode['wavmode'].to_string(index=False)

it should return 'm1' instead it returns ' m1'
This worked until version 0.23.0 and I reported it on #24980

@WillAyd
Copy link
Member

WillAyd commented Feb 12, 2020

This hasn't gotten traction in a while so closing as stale, but @charlesdong1991 or @simontorres ping if you plan to pick up and can continue from there

@WillAyd WillAyd closed this Feb 12, 2020
simontorres pushed a commit to simontorres/goodman_pipeline that referenced this pull request Oct 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Output-Formatting __repr__ of pandas objects, to_string
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug on .to_string(index=False)
5 participants