I have a dataframe from which, on two columns, I do some difference on dates:
difference=(df["date1"]-df["date2"]).dt.days
then I try to append it to existing dataframe, I get error messages. If I do:
df.assign(difference)
i get:
TypeError: assign() takes 1 positional argument but 2 were given
if I do:
df["Diference value"]=difference
i get:
A value is trying to be set on a copy of a slice from a DataFrame. Try using
.loc[row_indexer,col_indexer] = value
instead
in both cases last row is filled with NaN.
Anyway, I go along with this new dataframe, but when I try to groupby (that works fine) and get_group("Diference value")
I get:
> --------------------------------------------------------------------------- KeyError Traceback (most recent call
> last) <ipython-input-46-71486a5f3be6> in <module>
> ----> 1 dias=sectores.get_group("Difference value")
>
> D:\ArchivosProgramas\Anaconda\envs\pandas_playground\lib\site-packages\pandas\core\groupby\groupby.py
> in get_group(self, name, obj)
> 685 inds = self._get_index(name)
> 686 if not len(inds):
> --> 687 raise KeyError(name)
> 688
> 689 return obj._take_with_is_copy(inds, axis=self.axis)
>
> KeyError: 'Difference value'
I don't know where the error starts and how to fix it. All I need is this dataframe with that new column and then do grouping normally. I´ve been all day long trying to solve it. Any help is appreciated. Thanks.