問題描述
您好,我有以下數據框
df =
Record_ID Time
94704 2014-03-10 07:19:19.647342
94705 2014-03-10 07:21:44.479363
94706 2014-03-10 07:21:45.479581
94707 2014-03-10 07:21:54.481588
94708 2014-03-10 07:21:55.481804
有可能有以下嗎?
df1 =
Record_ID Time
94704 2014-03-10 07:19:19
94705 2014-03-10 07:21:44
94706 2014-03-10 07:21:45
94707 2014-03-10 07:21:54
94708 2014-03-10 07:21:55
推薦答案
你可以轉換底層 datetime64[ns]
值使用 astype
轉換為 datetime64[s]
值:
You could convert the underlying datetime64[ns]
values to datetime64[s]
values using astype
:
In [11]: df['Time'] = df['Time'].astype('datetime64[s]')
In [12]: df
Out[12]:
Record_ID Time
0 94704 2014-03-10 07:19:19
1 94705 2014-03-10 07:21:44
2 94706 2014-03-10 07:21:45
3 94707 2014-03-10 07:21:54
4 94708 2014-03-10 07:21:55
請注意,由于 Pandas 系列和 DataFrames 將所有日期時間值存儲為 datetime64[ns]
這些 datetime64[s]
值會自動轉換回 datetime64[ns]
,因此最終結果仍存儲為 datetime64[ns]
值,但對 astype
的調用會導致秒的小數部分被刪除.
Note that since Pandas Series and DataFrames store all datetime values as datetime64[ns]
these datetime64[s]
values are automatically converted back to datetime64[ns]
, so the end result is still stored as datetime64[ns]
values, but the call to astype
causes the fractional part of the seconds to be removed.
如果您希望有一個 datetime64[s]
值的 NumPy 數組,您可以使用 df['Time'].values.astype('datetime64[s]')代碼>.
If you wish to have a NumPy array of datetime64[s]
values, you could use df['Time'].values.astype('datetime64[s]')
.
這篇關于Python:降低精度 pandas 時間戳數據幀的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!