Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F-string with format modifiers in pandas expression triggers TypeError in Series.__format__

When I use f-string with format modifiers in expression using pandas Series - I get TypeError. However same string expression works fine with regular data. Is it possible to do this in pandas at all?

works with regular data:

episode = 42
frame = 4242
f"e{episode:03}_f{frame:06}.jpg"
> 'e042_f004242.jpg'

fails with pandas.Series:

df['filename'] = f"e{df.episode:03}_f{df.frame:05}_i{df.item:05}.jpg"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-84-73001803277d> in <module>
----> 1 df['filename'] = f"e{df.episode:03}_f{df.frame:05}_i{df.item:05}.jpg"
TypeError: unsupported format string passed to Series.__format__

The TypeError shows when I pass format modifiers in f-string. Any kind, including {df.area:.2f}. However without format modifiers I get no error but receive something useless like e0 215\n1 1\n2 1\n3

I have workarounds, but would like to use f-strings as they are neat.

pd.__version__
> '0.25.3'

df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 84604 entries, 0 to 84603
Data columns (total 5 columns):
episode     84604 non-null int32
frame       84604 non-null int64
item        84604 non-null int64
img_size    84604 non-null float64
bounds      84604 non-null object
dtypes: float64(1), int32(1), int64(2), object(1)
memory usage: 2.9+ MB
like image 295
Poe Dator Avatar asked Nov 26 '25 04:11

Poe Dator


1 Answers

Unfortunately f-strings not working if pass columns of DataFrame, is necesary processing by scalars in DataFrame.apply with x['item'] because item is valid pandas function Series.item and x.item here raise error:

df = pd.DataFrame({'episode':[42, 21], 'frame':[4242,4248], 'item':[20,563]})

df['filename']=df.apply(lambda x: f"e{x.episode:03}_f{x.frame:05}_i{x['item']:05}.jpg", axis=1)
print (df)
   episode  frame  item                filename
0       42   4242    20  e042_f04242_i00020.jpg
1       21   4248   563  e021_f04248_i00563.jpg

Or in list comprehension:

df['filename'] = [f"e{e:03}_f{f:05}_i{i:05}.jpg" 
                  for e,f,i  
                  in zip(df.episode, df.frame, df.item)]

However f-strings work fine with pd.Series without format modifiers: df['filename'] = f"e{df.episode}_f{df.frame}_i{df.item}.jpg" works OK.

I think not, because Series, so output is:

df['filename'] = f"e{df.episode}_f{df.frame}_i{df.item}.jpg"
print (df)
   episode  frame  item                                           filename
0       42   4242    20  e0    42\n1    21\nName: episode, dtype: int64...
1       21   4248   563  e0    42\n1    21\nName: episode, dtype: int64...
like image 86
jezrael Avatar answered Nov 27 '25 21:11

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!