Skip to content

Commit 7d43b2d

Browse files
authored
BUG: copying from Jupyter table to excel misaligns structure (#40356)
1 parent 500c4ae commit 7d43b2d

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ Other
679679
- Bug in :class:`Styler` where rendered HTML was missing a column class identifier for certain header cells (:issue:`39716`)
680680
- Bug in :meth:`Styler.background_gradient` where text-color was not determined correctly (:issue:`39888`)
681681
- Bug in :class:`Styler` where multiple elements in CSS-selectors were not correctly added to ``table_styles`` (:issue:`39942`)
682+
- Bug in :class:`.Styler` where copying from Jupyter dropped top left cell and misaligned headers (:issue:`12147`)
682683
- Bug in :meth:`DataFrame.equals`, :meth:`Series.equals`, :meth:`Index.equals` with object-dtype containing ``np.datetime64("NaT")`` or ``np.timedelta64("NaT")`` (:issue:`39650`)
683684
- Bug in :func:`pandas.util.show_versions` where console JSON output was not proper JSON (:issue:`39701`)
684685
- Bug in :meth:`DataFrame.convert_dtypes` incorrectly raised ValueError when called on an empty DataFrame (:issue:`40393`)

pandas/io/formats/style.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ def _translate(self):
359359

360360
DATA_CLASS = "data"
361361
BLANK_CLASS = "blank"
362-
BLANK_VALUE = ""
362+
BLANK_VALUE = " "
363363

364364
# mapping variables
365365
ctx = self.ctx # td css styles from apply() and applymap()

pandas/tests/io/formats/style/test_style.py

+20-19
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def h(x, foo="bar"):
3838
{"f": [1.0, 2.0], "o": ["a", "b"], "c": pd.Categorical(["a", "b"])}
3939
),
4040
]
41+
self.blank_value = " "
4142

4243
def test_init_non_pandas(self):
4344
msg = "``data`` must be a Series or DataFrame"
@@ -255,9 +256,9 @@ def test_empty_index_name_doesnt_display(self):
255256
{
256257
"class": "blank level0",
257258
"type": "th",
258-
"value": "",
259+
"value": self.blank_value,
259260
"is_visible": True,
260-
"display_value": "",
261+
"display_value": self.blank_value,
261262
},
262263
{
263264
"class": "col_heading level0 col0",
@@ -295,8 +296,8 @@ def test_index_name(self):
295296
{
296297
"class": "blank level0",
297298
"type": "th",
298-
"value": "",
299-
"display_value": "",
299+
"value": self.blank_value,
300+
"display_value": self.blank_value,
300301
"is_visible": True,
301302
},
302303
{
@@ -316,8 +317,8 @@ def test_index_name(self):
316317
],
317318
[
318319
{"class": "index_name level0", "type": "th", "value": "A"},
319-
{"class": "blank col0", "type": "th", "value": ""},
320-
{"class": "blank col1", "type": "th", "value": ""},
320+
{"class": "blank col0", "type": "th", "value": self.blank_value},
321+
{"class": "blank col1", "type": "th", "value": self.blank_value},
321322
],
322323
]
323324

@@ -333,15 +334,15 @@ def test_multiindex_name(self):
333334
{
334335
"class": "blank",
335336
"type": "th",
336-
"value": "",
337-
"display_value": "",
337+
"value": self.blank_value,
338+
"display_value": self.blank_value,
338339
"is_visible": True,
339340
},
340341
{
341342
"class": "blank level0",
342343
"type": "th",
343-
"value": "",
344-
"display_value": "",
344+
"value": self.blank_value,
345+
"display_value": self.blank_value,
345346
"is_visible": True,
346347
},
347348
{
@@ -355,7 +356,7 @@ def test_multiindex_name(self):
355356
[
356357
{"class": "index_name level0", "type": "th", "value": "A"},
357358
{"class": "index_name level1", "type": "th", "value": "B"},
358-
{"class": "blank col0", "type": "th", "value": ""},
359+
{"class": "blank col0", "type": "th", "value": self.blank_value},
359360
],
360361
]
361362

@@ -970,16 +971,16 @@ def test_mi_sparse(self):
970971
{
971972
"type": "th",
972973
"class": "blank",
973-
"value": "",
974+
"value": self.blank_value,
974975
"is_visible": True,
975-
"display_value": "",
976+
"display_value": self.blank_value,
976977
},
977978
{
978979
"type": "th",
979980
"class": "blank level0",
980-
"value": "",
981+
"value": self.blank_value,
981982
"is_visible": True,
982-
"display_value": "",
983+
"display_value": self.blank_value,
983984
},
984985
{
985986
"type": "th",
@@ -1013,7 +1014,7 @@ def test_mi_sparse_index_names(self):
10131014
expected = [
10141015
{"class": "index_name level0", "value": "idx_level_0", "type": "th"},
10151016
{"class": "index_name level1", "value": "idx_level_1", "type": "th"},
1016-
{"class": "blank col0", "value": "", "type": "th"},
1017+
{"class": "blank col0", "value": self.blank_value, "type": "th"},
10171018
]
10181019

10191020
assert head == expected
@@ -1034,8 +1035,8 @@ def test_mi_sparse_column_names(self):
10341035
expected = [
10351036
{
10361037
"class": "blank",
1037-
"value": "",
1038-
"display_value": "",
1038+
"value": self.blank_value,
1039+
"display_value": self.blank_value,
10391040
"type": "th",
10401041
"is_visible": True,
10411042
},
@@ -1343,7 +1344,7 @@ def test_w3_html_format(self):
13431344
<caption>A comprehensive test</caption>
13441345
<thead>
13451346
<tr>
1346-
<th class="blank level0" ></th>
1347+
<th class="blank level0" >&nbsp;</th>
13471348
<th class="col_heading level0 col0" >A</th>
13481349
</tr>
13491350
</thead>

0 commit comments

Comments
 (0)