Skip to content
Advertisement

TypeError: TimeGrouper.__init__() got multiple values for argument ‘freq’

What am I doing wrong?

This is all the code needed to reproduce.

JavaScript

Result:

JavaScript

Pandas version 1.5.1, Python version 3.10.6.

Advertisement

Answer

This seems to be a bug

It looks like the weirdness is because Grouper.__new__() instantiates a TimeGrouper if you pass freq as a kwarg, but not if you pass freq as a positional argument. I don’t know why it does that, and it’s not documented, so it seems like a bug.

The reason for the error is that TimeGrouper.__init__()‘s first parameter is freq, not key. (In fact it doesn’t even have a key parameter; it specifies **kwargs, which are then sent to Grouper.__init__() at the end.)

Workaround

Pass key as a kwarg too:

JavaScript

All-positional syntax is also broken

In cottontail’s answer, they suggested using positional arguments, with None for level, but this doesn’t work fully. For example, you can’t specify an origin:

JavaScript
JavaScript

(I’m using Python 3.9 and Pandas 1.4.4, so the error might look a bit different, but the same error should occur on your version.)

Even worse, the resulting Grouper doesn’t work, for example:

JavaScript
JavaScript

Compared to:

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