Skip to content

Commit

Permalink
Fixed issue: there was no need to encode to bytes to apply string for…
Browse files Browse the repository at this point in the history
…matting
  • Loading branch information
Sylvain MARIE committed May 14, 2023
1 parent 227f188 commit d44a4c9
Showing 1 changed file with 3 additions and 21 deletions.
24 changes: 3 additions & 21 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,6 @@ cdef str period_format(
object fast_fmt=None,
object fast_loc_am=None,
object fast_loc_pm=None,
bint preencoded_str=False
):
"""Important: please provide a dummy non-None fmt if fast_fmt is non-None"""

Expand Down Expand Up @@ -1240,19 +1239,11 @@ cdef str period_format(
elif fast_fmt is not None:
# A custom format is requested using python string formatting

if not preencoded_str:
# Encode strings using current locale, in case they contain non-utf8 chars
if isinstance(fast_fmt, str):
fast_fmt = <bytes>util.string_encode_locale(fast_fmt)
if isinstance(fast_loc_am, str):
fast_loc_am = <bytes>util.string_encode_locale(fast_loc_am)
if isinstance(fast_loc_pm, str):
fast_loc_pm = <bytes>util.string_encode_locale(fast_loc_pm)

# Get the quarter and fiscal year
quarter = get_yq(value, freq, &dts2)

# Finally use the string template
# Finally use the string template. Note: handling of non-utf8 chars is directly
# done in python here, no need to encode as for c-strftime
y = dts.year
h = dts.hour
return fast_fmt % {
Expand All @@ -1275,7 +1266,7 @@ cdef str period_format(
else:
# A custom format is requested using strftime (slower)

if not preencoded_str and isinstance(fmt, str):
if isinstance(fmt, str):
# Encode using current locale, in case fmt contains non-utf8 chars
fmt = <bytes>util.string_encode_locale(fmt)

Expand Down Expand Up @@ -1401,14 +1392,6 @@ def period_array_strftime(
except UnsupportedStrFmtDirective:
# Unsupported directive: fallback to standard `strftime`
pass
else:
# Encode strings using current locale, in case they contain non-utf8 chars
if isinstance(fast_fmt, str):
fast_fmt = <bytes>util.string_encode_locale(fast_fmt)
if isinstance(fast_loc_am, str):
fast_loc_am = <bytes>util.string_encode_locale(fast_loc_am)
if isinstance(fast_loc_pm, str):
fast_loc_pm = <bytes>util.string_encode_locale(fast_loc_pm)

for i in range(n):
# Analogous to: ordinal = values[i]
Expand All @@ -1433,7 +1416,6 @@ def period_array_strftime(
fast_fmt,
fast_loc_am,
fast_loc_pm,
True,
)

# Analogous to: ordinals[i] = ordinal
Expand Down

0 comments on commit d44a4c9

Please sign in to comment.