Skip to content
Advertisement

Get the last day of a year

I have a pandas df with a year column. I want to get the last day of that year. For example: 2020 –> 2020/12/31

I tried:

JavaScript

but I get this error: “cannot convert the series to <class ‘int’> “

What am I doing wrong? Thanks

PS I realized I could just do:

JavaScript

but I’m still wondering what was wrong in my previous code

Advertisement

Answer

Datetime.date works with single entries one at a time. When using data['year'].astype(int) you’re trying to pass an Series as an argument and not a single value (as it’s expected). For that to work, it’d be something like

JavaScript

But it’s somewhat inefficient, the second way you suggested would be faster.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement