如何将字符串的DataFrame列(以d.days,d.seconds,d.microseconds格式在正timedelta中)转换为datetime。 ?
先谢谢。!
答案 0 :(得分:1)
我认为unutbu这个答案特别有用。
def combine64(years, months=1, days=1, weeks=None, hours=None, minutes=None,
seconds=None, milliseconds=None, microseconds=None, nanoseconds=None):
"""
* Function: combine64
* Usage: df['combined']= combine64(df['Year'], df['Month'], df['Day']) . . .
* -------------------------------
* This function returns Datetime in the format YYYY-MM-DD:HH:mm:ss:ms:µs:ns
"""
years = np.asarray(years) - 1970
months = np.asarray(months) - 1
days = np.asarray(days) - 1
types = ('<M8[Y]', '<m8[M]', '<m8[D]', '<m8[W]', '<m8[h]',
'<m8[m]', '<m8[s]', '<m8[ms]', '<m8[us]', '<m8[ns]')
vals = (years, months, days, weeks, hours, minutes, seconds,
milliseconds, microseconds, nanoseconds)
return sum(np.asarray(v, dtype=t) for t, v in zip(types, vals)
if v is not None)