So beheben Sie TypeError: Datentyp wird mit einem Datetime-Objekt in Pandas nicht verstandenPython

Python-Programme
Anonymous
 So beheben Sie TypeError: Datentyp wird mit einem Datetime-Objekt in Pandas nicht verstanden

Post by Anonymous »

Ich arbeite mit einer Datumsspalte in Pandas. Ich habe eine Datumsspalte. Ich möchte nur das Jahr und den Monat als separate Spalte haben.

Das habe ich erreicht durch:

Code: Select all

df1["month"] = pd.to_datetime(Table_A_df['date']).dt.to_period('M')
Auf dem Ausdruck sieht es so aus:

Code: Select all

df1["month"]

Out:

0        2017-03
1        2017-03
2        2017-03
3        2017-03
4        2017-03
...
79638    2018-03
79639    2018-03
79640    2018-03
79641    2018-03
79642    2018-03
Name:   month, Length: 79643, dtype: period[M]
Meine Kundennummer sieht so aus:

Code: Select all

0        5094298f068196c5349d43847de5afc9125cf989
1                                             NaN
2                                             NaN
3        433fdf385e33176cf9b0d67ecf383aa928fa261c
4                                             NaN
...
79638    6836d8cdd9c6c537c702b35ccd972fae58070004
79639    bbc08d8abad5e699823f2f0021762797941679be
79640    39b5fdd28cb956053d3e4f3f0b884fb95749da8a
79641    3342d5b210274b01e947cc15531ad53fbe25435b
79642    b3f02d0768c0ba8334047d106eb759f3e80517ac
Name: customer_id, Length: 79643, dtype: object
Jetzt wird versucht, die Kunden-ID nach zu gruppieren und die Daten umzuwandeln.

Code: Select all

user_groups = df1.groupby("customer_id")["month"]

df1["Cohort_month"] = user_groups.transform("min")
Ich erhalte die folgende Fehlermeldung:

Code: Select all

TypeError: data type not understood
Vollständiger Fehler:

Code: Select all

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1 df1["Cohort_month"] = user_groups.transform("min")

C:\Users\Public\Anaconda\lib\site-packages\pandas\core\groupby\generic.py in transform(self, func, *args, **kwargs)
475         # result to the whole group. Compute func result
476         # and deal with possible broadcasting below.
--> 477         result = getattr(self, func)(*args, **kwargs)
478         return self._transform_fast(result, func)
479

C:\Users\Public\Anaconda\lib\site-packages\pandas\core\groupby\groupby.py in f(self, **kwargs)
1375                 # try a cython aggregation if we can
1376                 try:
-> 1377                     return self._cython_agg_general(alias, alt=npfunc, **kwargs)
1378                 except DataError:
1379                     pass

C:\Users\Public\Anaconda\lib\site-packages\pandas\core\groupby\groupby.py in _cython_agg_general(self, how, alt, numeric_only, min_count)
887
888             result, agg_names = self.grouper.aggregate(
--> 889                 obj._values, how, min_count=min_count
890             )
891

C:\Users\Public\Anaconda\lib\site-packages\pandas\core\groupby\ops.py in aggregate(self, values, how, axis, min_count)
568     ) -> Tuple[np.ndarray, Optional[List[str]]]:
569         return self._cython_operation(
--> 570             "aggregate", values, how, axis, min_count=min_count
571         )
572

C:\Users\Public\Anaconda\lib\site-packages\pandas\core\groupby\ops.py in _cython_operation(self, kind, values, how, axis, min_count, **kwargs)
560             result = type(orig_values)(result.astype(np.int64), dtype=orig_values.dtype)
561         elif is_datetimelike and kind == "aggregate":
--> 562             result = result.astype(orig_values.dtype)
563
564         return result, names

TypeError: data type not understood
Das hat früher funktioniert, als ich 1 als Tag hatte, aber als ich nur Jahr und Monat gemacht habe. Ich erhalte eine Fehlermeldung. Gibt es eine Lösung dafür?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post