Skip to content
Merged
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
12 changes: 11 additions & 1 deletion src/spaceone/inventory/manager/export_manager/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import tempfile
import logging
from datetime import datetime
import pandas as pd
import re
from datetime import datetime
from typing import Any
from openpyxl.styles import Font, Border, PatternFill, Alignment, Side

from spaceone.core.manager import BaseManager
Expand Down Expand Up @@ -75,12 +77,20 @@ def _change_sheet_name(name: str) -> str:
def _get_default_font(is_header: bool = False) -> Font:
return Font(size=12, bold=is_header, color="FFFFFF" if is_header else "000000")

@staticmethod
def _sanitize_string(value: Any) -> Any:
illegal_characters_re = re.compile(r"[\000-\010\013\014\016-\037]")
if isinstance(value, str):
value = illegal_characters_re.sub("", value)
return value

@staticmethod
def _write_excel_file(
writer: pd.ExcelWriter, df: pd.DataFrame, sheet_name: str, title: str = None
) -> None:
start_row = 1 if title else 0

df = df.map(ExportManager._sanitize_string)
df.to_excel(writer, sheet_name=sheet_name, index=False, startrow=start_row)
Comment on lines +93 to 94
Copy link

Copilot AI Apr 29, 2025

Choose a reason for hiding this comment

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

The use of df.map is intended to apply the sanitation function to every cell in the DataFrame, but DataFrame.map operates on columns (or fails entirely) while DataFrame.applymap should be used for elementwise operations. Consider replacing it with df = df.applymap(ExportManager._sanitize_string).

Suggested change
df = df.map(ExportManager._sanitize_string)
df.to_excel(writer, sheet_name=sheet_name, index=False, startrow=start_row)
df = df.applymap(ExportManager._sanitize_string)

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

FutureWarning: DataFrame.applymap has been deprecated. Use DataFrame.map instead.

In Pandas versions prior to 2.1.0, DataFrame.applymap() was the correct method to apply an elementwise operation across a DataFrame.
However, since Pandas 2.1.0, applymap() has been deprecated and the recommended approach is to use DataFrame.map() for elementwise operations.
Therefore, if targeting Pandas 2.1.0 or newer, it is correct to replace applymap() with map().

ws = writer.sheets[sheet_name]

Expand Down
Loading