您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Pandas怎么將Timestamp轉(zhuǎn)為datetime類型”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
在Pandas中我們?cè)谔幚頃r(shí)間序列的時(shí)候常用的方法有:
pd.to_datetime()
pd.date_range()
# pd.date_range() index = pd.date_range("20210101",periods=20) index Out[29]: DatetimeIndex(['2021-01-01', '2021-01-02', '2021-01-03', '2021-01-04', '2021-01-05', '2021-01-06', '2021-01-07', '2021-01-08', '2021-01-09', '2021-01-10', '2021-01-11', '2021-01-12', '2021-01-13', '2021-01-14', '2021-01-15', '2021-01-16', '2021-01-17', '2021-01-18', '2021-01-19', '2021-01-20'], dtype='datetime64[ns]', freq='D') # pd.to_datetime() df = pd.DataFrame(data=range(20210101,20210128),columns=["period"]) df["aa"] = pd.to_datetime(df["period"],format="%Y%m%d") df Out[24]: period aa 0 20210101 2021-01-01 1 20210102 2021-01-02 2 20210103 2021-01-03 3 20210104 2021-01-04 4 20210105 2021-01-05 5 20210106 2021-01-06 6 20210107 2021-01-07 7 20210108 2021-01-08 8 20210109 2021-01-09 9 20210110 2021-01-10 10 20210111 2021-01-11 11 20210112 2021-01-12 12 20210113 2021-01-13 13 20210114 2021-01-14 14 20210115 2021-01-15 15 20210116 2021-01-16 16 20210117 2021-01-17 17 20210118 2021-01-18 18 20210119 2021-01-19 19 20210120 2021-01-20 20 20210121 2021-01-21 21 20210122 2021-01-22 22 20210123 2021-01-23 23 20210124 2021-01-24 24 20210125 2021-01-25 25 20210126 2021-01-26 26 20210127 2021-01-27 index[1] Out[30]: Timestamp('2021-01-02 00:00:00', freq='D') df["aa"][1] Out[31]: Timestamp('2021-01-02 00:00:00') df["aa"][1] == index[1] Out[32]: True type(df["aa"][1]) Out[33]: pandas._libs.tslibs.timestamps.Timestamp type(index[1]) Out[34]: pandas._libs.tslibs.timestamps.Timestamp
從上面代碼可以看出,pandas中的時(shí)間格式是pandas._libs.tslibs.timestamps.Timestamp
但是python中常用的時(shí)間格式是datetime.datetime
to_pydatetime()
t = datetime(2021,1,2) type(t) Out[54]: datetime.datetime t Out[55]: datetime.datetime(2021, 1, 2, 0, 0) r = (index[1].to_pydatetime()) type(r) Out[57]: datetime.datetime t == r Out[58]: True
將pandas Timestamp 轉(zhuǎn)為 datetime 類型
In [11]: ts = pd.Timestamp('2014-01-23 00:00:00', tz=None) In [12]: ts.to_pydatetime() Out[12]: datetime.datetime(2014, 1, 23, 0, 0)
It's also available on a DatetimeIndex rng = pd.date_range('1/10/2011', periods=3, freq='D') rng.to_pydatetime() Out[60]: array([datetime.datetime(2011, 1, 10, 0, 0), datetime.datetime(2011, 1, 11, 0, 0), datetime.datetime(2011, 1, 12, 0, 0)], dtype=object)
官方文檔: https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#from-timestamps-to-epoch
最近需要提取某一天的時(shí)刻距離0:00的分鐘數(shù),找了文檔之后想到這樣一個(gè)辦法:
In [64]: stamps = pd.date_range('2012-10-08 18:15:05', periods=4, freq='h') In [65]: stamps Out[65]: DatetimeIndex(['2012-10-08 18:15:05', '2012-10-08 19:15:05', '2012-10-08 20:15:05', '2012-10-08 21:15:05'], dtype='datetime64[ns]', freq='D')
先得到距離1970-01-01的秒數(shù)
In [66]: (stamps - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s') Out[66]: Int64Index([1349720105, 1349723705, 1349727305, 1349730905], dtype='int64')
對(duì)天取余,得到距離0:00的秒數(shù)
In [67]: (stamps - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s') % 86400 Out[67]: Int64Index([65705, 69305, 72905, 76505], dtype='int64')
取距離0:00的分鐘數(shù)
In [68]: (stamps - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s') % 86400 /60 Out[68]: Int64Index([1095.0833333333333, 1155.0833333333333, 1215.0833333333333, 1275.0833333333333], dtype='float64')
同樣的,也可以取小時(shí)數(shù)
In [69]: (stamps - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s') % 86400 /3600 Out[68]: Int64Index([18.25138888888889, 19.25138888888889, 20.25138888888889, 21.25138888888889], dtype='float64')
取小時(shí)整數(shù)–當(dāng)然取小時(shí)整數(shù)也有別的方法。
In [70]: (stamps - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s') % 86400 //3600 Out[70]: Int64Index([18, 19, 20, 21], dtype='int64')
“Pandas怎么將Timestamp轉(zhuǎn)為datetime類型”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。