Skip to content

BUG: fixes data table alignment when emojis are present #58311

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1713,6 +1713,16 @@ def _make_fixed_width(
if len(strings) == 0 or justify == "all":
return strings

# regex to identify emojis
emoji_pattern = re.compile(
r"[\U0001F600-\U0001F64F]|[\U0001F300-\U0001F5FF]|[\U0001F680-\U0001F6FF]|[\U0001F780-\U0001F7FF]",
flags=re.UNICODE,
)

emoji_count = sum(len(emoji_pattern.findall(s)) for s in strings)
if emoji_count > 0:
set_option("display.unicode.east_asian_width", True)
Copy link
Member

Choose a reason for hiding this comment

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

Modifying global setting silently for users is probably not a great user experience as it can lead to silent bugs.


if adj is None:
adjustment = printing.get_adjustment()
else:
Expand Down
44 changes: 44 additions & 0 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,50 @@ def test_max_rows_fitted(self, length, min_rows, max_rows, expected):
result = formatter.max_rows_fitted
assert result == expected

def test_display_settings_with_emojis(self):
# Define the data for DataFrame
example = {
"normal_col": [1, 2, 3],
"text_col": ["hello world"] * 3,
"emoji_col_A": ["🟩 hello world"] * 3,
"emoji_col_B": ["🟥 hello world"] * 3,
}
# Create DataFrame
df = DataFrame(example)
output = repr(df)
expected_output = """ normal_col text_col emoji_col_A emoji_col_B
0 1 hello world 🟩 hello world 🟥 hello world
1 2 hello world 🟩 hello world 🟥 hello world
2 3 hello world 🟩 hello world 🟥 hello world"""

assert output == expected_output

# Reset options to defaults
reset_option("display.max_rows")
reset_option("display.max_columns")
reset_option("display.width")

example2 = {
"normal_col": [1, 2, 3],
"text_col": ["hello world"] * 3,
"emoji_col_A": ["🙏🙏 hello world"] * 3,
}
# Create DataFrame
df = DataFrame(example2)
output = repr(df)
expected_output2 = """ normal_col text_col emoji_col_A
0 1 hello world 🙏🙏 hello world
1 2 hello world 🙏🙏 hello world
2 3 hello world 🙏🙏 hello world"""
# Check if the actual output matches expected output
assert output == expected_output2

# Reset options to defaults
reset_option("display.max_rows")
reset_option("display.max_columns")
reset_option("display.width")
reset_option("display.unicode.east_asian_width")


def gen_series_formatting():
s1 = Series(["a"] * 100)
Expand Down