1

I've run into this weird error and I'm wondering whether anyone has a clue what's going on.

I'm trying to print the minimal date from a date column in Pandas series. However, the following is raising an invalid syntax error:

print(f'{df_raw['POSTING_DATE'].min()}')

This does work, though:

min_date = df_raw['POSTING_DATE'].min()
print(f'{min_date}')

Using .format() also works. Obviously I can use a workaround here but I was just wondering why the f-string syntax doesn't work in this case. I thought the f-strings should be able to handle similar expressions. I'm using Python 3.6.9.

1 Answers1

4

Use " as:

print(f"{df_raw['POSTING_DATE'].min()}")

Update:

Ideally, we could use \ to escape quotes but f-strings does not support using \ in it so this wouldn't work with f-strings

print(f'{df_raw[\'POSTING_DATE\'].min()}')
Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35