Skip to content

Commit 5b74382

Browse files
committed
BUG: fix data table alignment when emojis are present (pandas-dev#58098)
1 parent 4241ba5 commit 5b74382

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

pandas/io/formats/format.py

+10
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,16 @@ def _make_fixed_width(
17131713
if len(strings) == 0 or justify == "all":
17141714
return strings
17151715

1716+
# regex to identify emojis
1717+
emoji_pattern = re.compile(
1718+
r"[\U0001F600-\U0001F64F]|[\U0001F300-\U0001F5FF]|[\U0001F680-\U0001F6FF]|[\U0001F780-\U0001F7FF]",
1719+
flags=re.UNICODE,
1720+
)
1721+
1722+
emoji_count = sum(len(emoji_pattern.findall(s)) for s in strings)
1723+
if emoji_count > 0:
1724+
set_option("display.unicode.east_asian_width", True)
1725+
17161726
if adj is None:
17171727
adjustment = printing.get_adjustment()
17181728
else:

pandas/tests/io/formats/test_format.py

+44
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,50 @@ def test_max_rows_fitted(self, length, min_rows, max_rows, expected):
13211321
result = formatter.max_rows_fitted
13221322
assert result == expected
13231323

1324+
def test_display_settings_with_emojis(self):
1325+
# Define the data for DataFrame
1326+
example = {
1327+
"normal_col": [1, 2, 3],
1328+
"text_col": ["hello world"] * 3,
1329+
"emoji_col_A": ["🟩 hello world"] * 3,
1330+
"emoji_col_B": ["🟥 hello world"] * 3,
1331+
}
1332+
# Create DataFrame
1333+
df = DataFrame(example)
1334+
output = repr(df)
1335+
expected_output = """ normal_col text_col emoji_col_A emoji_col_B
1336+
0 1 hello world 🟩 hello world 🟥 hello world
1337+
1 2 hello world 🟩 hello world 🟥 hello world
1338+
2 3 hello world 🟩 hello world 🟥 hello world"""
1339+
1340+
assert output == expected_output
1341+
1342+
# Reset options to defaults
1343+
reset_option("display.max_rows")
1344+
reset_option("display.max_columns")
1345+
reset_option("display.width")
1346+
1347+
example2 = {
1348+
"normal_col": [1, 2, 3],
1349+
"text_col": ["hello world"] * 3,
1350+
"emoji_col_A": ["🙏🙏 hello world"] * 3,
1351+
}
1352+
# Create DataFrame
1353+
df = DataFrame(example2)
1354+
output = repr(df)
1355+
expected_output2 = """ normal_col text_col emoji_col_A
1356+
0 1 hello world 🙏🙏 hello world
1357+
1 2 hello world 🙏🙏 hello world
1358+
2 3 hello world 🙏🙏 hello world"""
1359+
# Check if the actual output matches expected output
1360+
assert output == expected_output2
1361+
1362+
# Reset options to defaults
1363+
reset_option("display.max_rows")
1364+
reset_option("display.max_columns")
1365+
reset_option("display.width")
1366+
reset_option("display.unicode.east_asian_width")
1367+
13241368

13251369
def gen_series_formatting():
13261370
s1 = Series(["a"] * 100)

0 commit comments

Comments
 (0)