久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <small id='7gKBp'></small><noframes id='7gKBp'>

  • <i id='7gKBp'><tr id='7gKBp'><dt id='7gKBp'><q id='7gKBp'><span id='7gKBp'><b id='7gKBp'><form id='7gKBp'><ins id='7gKBp'></ins><ul id='7gKBp'></ul><sub id='7gKBp'></sub></form><legend id='7gKBp'></legend><bdo id='7gKBp'><pre id='7gKBp'><center id='7gKBp'></center></pre></bdo></b><th id='7gKBp'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='7gKBp'><tfoot id='7gKBp'></tfoot><dl id='7gKBp'><fieldset id='7gKBp'></fieldset></dl></div>

      <tfoot id='7gKBp'></tfoot>

          <bdo id='7gKBp'></bdo><ul id='7gKBp'></ul>
        <legend id='7gKBp'><style id='7gKBp'><dir id='7gKBp'><q id='7gKBp'></q></dir></style></legend>

        Python pandas 通過 dt 訪問器有效地將日期時間轉(zhuǎn)換

        Python pandas convert datetime to timestamp effectively through dt accessor(Python pandas 通過 dt 訪問器有效地將日期時間轉(zhuǎn)換為時間戳)

            <tbody id='CwYkm'></tbody>

          <legend id='CwYkm'><style id='CwYkm'><dir id='CwYkm'><q id='CwYkm'></q></dir></style></legend>

            1. <tfoot id='CwYkm'></tfoot>
              <i id='CwYkm'><tr id='CwYkm'><dt id='CwYkm'><q id='CwYkm'><span id='CwYkm'><b id='CwYkm'><form id='CwYkm'><ins id='CwYkm'></ins><ul id='CwYkm'></ul><sub id='CwYkm'></sub></form><legend id='CwYkm'></legend><bdo id='CwYkm'><pre id='CwYkm'><center id='CwYkm'></center></pre></bdo></b><th id='CwYkm'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='CwYkm'><tfoot id='CwYkm'></tfoot><dl id='CwYkm'><fieldset id='CwYkm'></fieldset></dl></div>

              <small id='CwYkm'></small><noframes id='CwYkm'>

                <bdo id='CwYkm'></bdo><ul id='CwYkm'></ul>
                  本文介紹了Python pandas 通過 dt 訪問器有效地將日期時間轉(zhuǎn)換為時間戳的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我有一個包含一些(數(shù)億)行的數(shù)據(jù)框.我想有效地將??日期時間轉(zhuǎn)換為時間戳.我該怎么做?

                  我的示例df:

                  df = pd.DataFrame(index=pd.DatetimeIndex(start=dt.datetime(2016,1,1,0,0,1),結(jié)束=dt.datetime(2016,1,2,0,0,1), 頻率='H')).reset_index().rename(columns={'index':'datetime'})df.head()約會時間0 2016-01-01 00:00:011 2016-01-01 01:00:012 2016-01-01 02:00:013 2016-01-01 03:00:014 2016-01-01 04:00:01

                  現(xiàn)在我使用 .apply() 將日期時間逐個轉(zhuǎn)換為時間戳值,但如果我有一些(數(shù)億)行,則需要很長時間(幾個小時):

                  df['ts'] = df[['datetime']].apply(lambda x: x[0].timestamp(), axis=1).astype(int)df.head()日期時間 ts0 2016-01-01 00:00:01 14516028011 2016-01-01 01:00:01 14516064012 2016-01-01 02:00:01 14516100013 2016-01-01 03:00:01 14516136014 2016-01-01 04:00:01 1451617201

                  上面的結(jié)果就是我想要的.

                  如果我嘗試使用 pandas.Series.dt 訪問器,則會收到錯誤消息:

                  df['ts'] = df['datetime'].dt.timestamp

                  <塊引用>

                  AttributeError: 'DatetimeProperties' 對象沒有屬性'時間戳'

                  如果我嘗試創(chuàng)建例如.使用 .dt 訪問器的日期時間的日期部分比使用 .apply() 快得多:

                  df['date'] = df['datetime'].dt.datedf.head()日期時間 ts 日期0 2016-01-01 00:00:01 1451602801 2016-01-011 2016-01-01 01:00:01 1451606401 2016-01-012 2016-01-01 02:00:01 1451610001 2016-01-013 2016-01-01 03:00:01 1451613601 2016-01-014 2016-01-01 04:00:01 1451617201 2016-01-01

                  我想要類似時間戳的東西...

                  但我不太了解官方文檔:它談到轉(zhuǎn)換為時間戳" 但我沒有看到任何時間戳;它只是談?wù)撌褂?pd.to_datetime() 轉(zhuǎn)換為日期時間,而不是時間戳...

                  pandas.Timestamp 構(gòu)造函數(shù)也不起作用(返回以下錯誤):

                  df['ts2'] = pd.Timestamp(df['datetime'])

                  <塊引用>

                  TypeError:無法將輸入轉(zhuǎn)換為時間戳

                  pandas.Series.to_timestamp 也做出了我想要的完全不同的東西:

                  df['ts3'] = df['datetime'].to_timestampdf.head()日期時間 ts ts30 2016-01-01 00:00:01 1451602801 <綁定方法 Series.to_timestamp of 0 2016...1 2016-01-01 01:00:01 1451606401 <綁定方法 Series.to_timestamp of 0 2016...2 2016-01-01 02:00:01 1451610001 <綁定方法 Series.to_timestamp of 0 2016...3 2016-01-01 03:00:01 1451613601 <綁定方法 Series.to_timestamp of 0 2016...4 2016-01-01 04:00:01 1451617201 <綁定方法 Series.to_timestamp of 0 2016...

                  謝謝!!

                  解決方案

                  我覺得你需要先轉(zhuǎn)換成 numpy array by values 并轉(zhuǎn)換為 int64 - 輸出在 ns,所以需要除以10 ** 9:

                  df['ts'] = df.datetime.values.astype(np.int64)//10 ** 9打印 (df)日期時間 ts0 2016-01-01 00:00:01 14516064011 2016-01-01 01:00:01 14516100012 2016-01-01 02:00:01 14516136013 2016-01-01 03:00:01 14516172014 2016-01-01 04:00:01 14516208015 2016-01-01 05:00:01 14516244016 2016-01-01 06:00:01 14516280017 2016-01-01 07:00:01 14516316018 2016-01-01 08:00:01 14516352019 2016-01-01 09:00:01 145163880110 2016-01-01 10:00:01 145164240111 2016-01-01 11:00:01 145164600112 2016-01-01 12:00:01 145164960113 2016-01-01 13:00:01 145165320114 2016-01-01 14:00:01 145165680115 2016-01-01 15:00:01 145166040116 2016-01-01 16:00:01 145166400117 2016-01-01 17:00:01 145166760118 2016-01-01 18:00:01 145167120119 2016-01-01 19:00:01 145167480120 2016-01-01 20:00:01 145167840121 2016-01-01 21:00:01 145168200122 2016-01-01 22:00:01 145168560123 2016-01-01 23:00:01 145168920124 2016-01-02 00:00:01 1451692801

                  to_timestamp 用于將 從周期索引轉(zhuǎn)換為日期時間索引.

                  I have a dataframe with some (hundreds of) million of rows. And I want to convert datetime to timestamp effectively. How can I do it?

                  My sample df:

                  df = pd.DataFrame(index=pd.DatetimeIndex(start=dt.datetime(2016,1,1,0,0,1),
                      end=dt.datetime(2016,1,2,0,0,1), freq='H'))
                      .reset_index().rename(columns={'index':'datetime'})
                  
                  df.head()
                  
                               datetime
                  0 2016-01-01 00:00:01
                  1 2016-01-01 01:00:01
                  2 2016-01-01 02:00:01
                  3 2016-01-01 03:00:01
                  4 2016-01-01 04:00:01
                  

                  Now I convert datetime to timestamp value-by-value with .apply() but it takes a very long time (some hours) if I have some (hundreds of) million rows:

                  df['ts'] = df[['datetime']].apply(lambda x: x[0].timestamp(), axis=1).astype(int)
                  
                  df.head()
                  
                               datetime          ts
                  0 2016-01-01 00:00:01  1451602801
                  1 2016-01-01 01:00:01  1451606401
                  2 2016-01-01 02:00:01  1451610001
                  3 2016-01-01 03:00:01  1451613601
                  4 2016-01-01 04:00:01  1451617201
                  

                  The above result is what I want.

                  If I try to use the .dt accessor of pandas.Series then I get error message:

                  df['ts'] = df['datetime'].dt.timestamp
                  

                  AttributeError: 'DatetimeProperties' object has no attribute 'timestamp'

                  If I try to create eg. the date parts of datetimes with the .dt accessor then it is much more faster then using .apply():

                  df['date'] = df['datetime'].dt.date
                  
                  df.head()
                  
                               datetime          ts        date
                  0 2016-01-01 00:00:01  1451602801  2016-01-01
                  1 2016-01-01 01:00:01  1451606401  2016-01-01
                  2 2016-01-01 02:00:01  1451610001  2016-01-01
                  3 2016-01-01 03:00:01  1451613601  2016-01-01
                  4 2016-01-01 04:00:01  1451617201  2016-01-01
                  

                  I want something similar with timestamps...

                  But I don't really understand the official documentation: it talks about "Converting to Timestamps" but I don't see any timestamps there; it just talks about converting to datetime with pd.to_datetime() but not to timestamp...

                  pandas.Timestamp constructor also doesn't work (returns with the below error):

                  df['ts2'] = pd.Timestamp(df['datetime'])
                  

                  TypeError: Cannot convert input to Timestamp

                  pandas.Series.to_timestamp also makes something totally different that I want:

                  df['ts3'] = df['datetime'].to_timestamp
                  
                  df.head()
                  
                               datetime          ts                                                ts3
                  0 2016-01-01 00:00:01  1451602801  <bound method Series.to_timestamp of 0    2016...
                  1 2016-01-01 01:00:01  1451606401  <bound method Series.to_timestamp of 0    2016...
                  2 2016-01-01 02:00:01  1451610001  <bound method Series.to_timestamp of 0    2016...
                  3 2016-01-01 03:00:01  1451613601  <bound method Series.to_timestamp of 0    2016...
                  4 2016-01-01 04:00:01  1451617201  <bound method Series.to_timestamp of 0    2016...
                  

                  Thank you!!

                  解決方案

                  I think you need convert first to numpy array by values and cast to int64 - output is in ns, so need divide by 10 ** 9:

                  df['ts'] = df.datetime.values.astype(np.int64) // 10 ** 9
                  print (df)
                                datetime          ts
                  0  2016-01-01 00:00:01  1451606401
                  1  2016-01-01 01:00:01  1451610001
                  2  2016-01-01 02:00:01  1451613601
                  3  2016-01-01 03:00:01  1451617201
                  4  2016-01-01 04:00:01  1451620801
                  5  2016-01-01 05:00:01  1451624401
                  6  2016-01-01 06:00:01  1451628001
                  7  2016-01-01 07:00:01  1451631601
                  8  2016-01-01 08:00:01  1451635201
                  9  2016-01-01 09:00:01  1451638801
                  10 2016-01-01 10:00:01  1451642401
                  11 2016-01-01 11:00:01  1451646001
                  12 2016-01-01 12:00:01  1451649601
                  13 2016-01-01 13:00:01  1451653201
                  14 2016-01-01 14:00:01  1451656801
                  15 2016-01-01 15:00:01  1451660401
                  16 2016-01-01 16:00:01  1451664001
                  17 2016-01-01 17:00:01  1451667601
                  18 2016-01-01 18:00:01  1451671201
                  19 2016-01-01 19:00:01  1451674801
                  20 2016-01-01 20:00:01  1451678401
                  21 2016-01-01 21:00:01  1451682001
                  22 2016-01-01 22:00:01  1451685601
                  23 2016-01-01 23:00:01  1451689201
                  24 2016-01-02 00:00:01  1451692801
                  

                  to_timestamp is used for converting from period to datetime index.

                  這篇關(guān)于Python pandas 通過 dt 訪問器有效地將日期時間轉(zhuǎn)換為時間戳的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  python: Two modules and classes with the same name under different packages(python:不同包下同名的兩個模塊和類)
                  Configuring Python to use additional locations for site-packages(配置 Python 以使用站點包的其他位置)
                  How to structure python packages without repeating top level name for import(如何在不重復(fù)導(dǎo)入頂級名稱的情況下構(gòu)造python包)
                  Install python packages on OpenShift(在 OpenShift 上安裝 python 包)
                  How to refresh sys.path?(如何刷新 sys.path?)
                  Distribute a Python package with a compiled dynamic shared library(分發(fā)帶有已編譯動態(tài)共享庫的 Python 包)
                    • <bdo id='JwqNH'></bdo><ul id='JwqNH'></ul>

                      <i id='JwqNH'><tr id='JwqNH'><dt id='JwqNH'><q id='JwqNH'><span id='JwqNH'><b id='JwqNH'><form id='JwqNH'><ins id='JwqNH'></ins><ul id='JwqNH'></ul><sub id='JwqNH'></sub></form><legend id='JwqNH'></legend><bdo id='JwqNH'><pre id='JwqNH'><center id='JwqNH'></center></pre></bdo></b><th id='JwqNH'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='JwqNH'><tfoot id='JwqNH'></tfoot><dl id='JwqNH'><fieldset id='JwqNH'></fieldset></dl></div>
                        1. <legend id='JwqNH'><style id='JwqNH'><dir id='JwqNH'><q id='JwqNH'></q></dir></style></legend>
                              <tbody id='JwqNH'></tbody>
                            <tfoot id='JwqNH'></tfoot>

                            <small id='JwqNH'></small><noframes id='JwqNH'>

                            主站蜘蛛池模板: 日韩精品视频网 | 国产精品中文字幕在线 | 亚洲成网站 | 日韩毛片免费视频 | 欧美4p| 国产小视频在线看 | 99re视频这里只有精品 | 成年女人免费v片 | 亚洲精品99999| 亚洲一区国产 | 天天看天天干 | 色橹橹欧美在线观看视频高清 | 久久精品免费一区二区三 | 韩日av片| 成人亚洲一区 | 久久视频免费观看 | 久久久久久久一级 | 久久久久久电影 | 国产精品一区二区三区在线 | 超碰国产在线 | 国产精品女人久久久 | 久久久av中文字幕 | 亚洲xx在线 | 亚洲精品乱码久久久久久9色 | 亚洲3级| 免费一区 | 91免费入口 | 国产综合久久 | 国产色 | 精品中文字幕一区二区 | 97精品超碰一区二区三区 | 日韩在线观看一区二区三区 | 四虎午夜剧场 | www成人免费视频 | 欧美一区在线看 | 免费在线成人网 | 九九热在线免费视频 | 精品国产一区二区三区久久久四川 | 91视频进入 | 欧美成人免费在线视频 | 性视频一区 |