Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

python - String formatting of timedeltas in Pandas

I noticed that Pandas knows how to smartly format a timedelta object into a string.

In [1]: df[column][rows].max()
Out[1]: 
0   2 days, 02:08:07
dtype: timedelta64[ns]

When I try to do this manually I keep getting the string in nanoseconds.

In [2]: df[column][rows].max()[0]
Out[2]: numpy.timedelta64(180487000000000,'ns')

In [2]: str(df[column][rows].max()[0])
Out[2]: '180487000000000 nanoseconds'

I would rather not reinvent the wheel, so is there any way to access the string formatting method (or the string itself) that Pandas uses to show a timedelta object in x days, hh:mm:ss ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The function is located here:

pd.tslib.repr_timedelta64

In action:

In [11]: pd.tslib.repr_timedelta64(np.timedelta64(180487000000000,'ns'))
Out[11]: '2 days, 02:08:07'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...