Eine pandas DataFrame-Spalte duration
enthält timedelta64[ns]
wie gezeigt. Wie können Sie sie in Sekunden umwandeln?Konvertieren von timedelta64 [ns] -Spalte in Sekunden in Python Pandas DataFrame
0 00:20:32
1 00:23:10
2 00:24:55
3 00:13:17
4 00:18:52
Name: duration, dtype: timedelta64[ns]
Ich habe versucht, die folgende
print df[:5]['duration']/np.timedelta64(1, 's')
bekam aber den Fehler
Traceback (most recent call last):
File "test.py", line 16, in <module>
print df[0:5]['duration']/np.timedelta64(1, 's')
File "C:\Python27\lib\site-packages\pandas\core\series.py", line 130, in wrapper
"addition and subtraction, but the operator [%s] was passed" % name)
TypeError: can only operate on a timedeltas for addition and subtraction, but the operator [__div__] was passed
Auch
versuchtprint df[:5]['duration'].astype('timedelta64[s]')
erhielt aber den Fehler
Traceback (most recent call last):
File "test.py", line 17, in <module>
print df[:5]['duration'].astype('timedelta64[s]')
File "C:\Python27\lib\site-packages\pandas\core\series.py", line 934, in astype
values = com._astype_nansafe(self.values, dtype)
File "C:\Python27\lib\site-packages\pandas\core\common.py", line 1653, in _astype_nansafe
raise TypeError("cannot astype a timedelta from [%s] to [%s]" % (arr.dtype,dtype))
TypeError: cannot astype a timedelta from [timedelta64[ns]] to [timedelta64[s]]
dies ist fest Neue Releases; Wie im [Quellcode] (https://github.com/pydata/pandas/flame/778cfe43a4cef52442e00fe505290079a717f515/pandas/core/ops.py#L304), wenn beide Seiten timedelta sind, werden Subtraktion und _division_ unterstützt. dieser Abschnitt des Codes wurde im Jahr 2013 geändert –
@ behzad.nouri ist richtig, ich glaube, das wurde in> 0,13.1 behoben (vielleicht braucht 0,14), aber aktuell ist 0,15.0: http://pandas.pydata.org /pandas-docs/stable/timedeltas.html#frequency-conversion – Jeff
@ behzad.nouri Danke, Update auf Pandas 0.15.0 und numpy 1.9.0 hat es zur Arbeit gebracht. – Nyxynyx