{"id":1494,"date":"2021-07-02T08:31:12","date_gmt":"2021-07-02T08:31:12","guid":{"rendered":"https:\/\/www.ipcpu.com\/?p=1494"},"modified":"2021-08-02T08:31:51","modified_gmt":"2021-08-02T08:31:51","slug":"pandas-to_datetime","status":"publish","type":"post","link":"https:\/\/c.ipcpu.com\/2021\/07\/pandas-to_datetime\/","title":{"rendered":"pandas\u4e2d\u7684to_datetime\u51fd\u6570"},"content":{"rendered":"

\u4e00\u3001\u6982\u8ff0<\/h2>\n

\u5728\u65e5\u5e38\u6570\u636e\u5904\u7406\u4e2d\uff0c\u6700\u4e3a\u607c\u706b\u7684\u5c31\u662f\u65f6\u95f4\u7684\u5904\u7406\u3002\u65f6\u95f4\u53ef\u4ee5\u6709\u5f88\u591a\u79cd\u8868\u8fbe\u5f62\u5f0f\uff0c\u6bd4\u5982\u4e0b\u9762\u8fd9\u5f20\u56fe\uff0c\u6211\u4eec\u770b\u5230\u540c\u6837\u662f 2017 \u5e74 1 \u6708 5 \u65e5\uff0c\u53ef\u4ee5\u6709\u5f88\u591a\u79cd\u65f6\u95f4\u683c\u5f0f\uff0c\u6211\u4eec\u9700\u8981\u5148\u5c06\u683c\u5f0f\u7edf\u4e00\u624d\u80fd\u8fdb\u884c\u540e\u9762\u7684\u5de5\u4f5c\uff0cPandas \u63d0\u4f9b\u4e86\u8fd9\u4e2a\u51fd\u6570 to_datetime ().
\n<\/p>\n

\u4e8c\u3001\u7b80\u5355\u7528\u6cd5<\/h2>\n
\n
dates1 = ['2017-01-05', 'Jan 5, 2017', '01\/05\/2017', '2017.01.05', '2017\/01\/05', '20170105']\nprint(pd.to_datetime(dates1))\n\ndates2 = ['2017-01-05 2:30:00 PM', 'Jan 5, 2017 14:30:00', '01\/05\/2016', '2017.01.05', '2017\/01\/05', '20170105']\nprint(pd.to_datetime(dates2))<\/code><\/pre>\n<\/div>\n

\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff0c<\/p>\n

\n
DatetimeIndex(['2017-01-05', '2017-01-05', '2017-01-05', '2017-01-05',\n               '2017-01-05', '2017-01-05'],\n              dtype='datetime64[ns]', freq=None)\nDatetimeIndex(['2017-01-05 14:30:00', '2017-01-05 14:30:00',\n               '2016-01-05 00:00:00', '2017-01-05 00:00:00',\n               '2017-01-05 00:00:00', '2017-01-05 00:00:00'],\n              dtype='datetime64[ns]', freq=None)<\/code><\/pre>\n<\/div>\n

\u4e09\u3001\u9519\u8bef\u5904\u7406<\/h2>\n

\u5982\u679c\u6211\u4eec\u5904\u7406\u7684\u6570\u636e\u4e2d\u5305\u542b\u4e86\u4e00\u4e9b\u65e0\u6cd5\u8bc6\u522b\u7684\u65f6\u95f4\u683c\u5f0f\u6216\u8005\u662f\u5b57\u7b26\u4e32\uff0c\u5c31\u4f1a\u62a5\u9519\uff0c<\/p>\n

\n
#ignore \u5ffd\u7565\uff0c\u539f\u6837\u8f93\u51fa\uff1b raise \u76f4\u63a5\u62a5\u9519\uff0c\u9ed8\u8ba4\u884c\u4e3a\uff1b coerce\u5982\u679c\u51fa\u9519\uff0c\u8bbe\u7f6e\u6210NaT\nerrors{\u2018ignore\u2019, \u2018raise\u2019, \u2018coerce\u2019}, default \u2018raise\u2019<\/code><\/pre>\n<\/div>\n

\u6211\u4eec\u6765\u770b\u4e2a\u4f8b\u5b50<\/p>\n

\n
#\u4ee3\u7801\ndates3 = ['2017-01-05 2:30:00 PM', 'Jan 5, 2017 14:30:00', 'ABCDEFG', '2017.01.05', '2017\/01\/05', '20170105']\nprint(pd.to_datetime(dates3, errors='coerce'))\n#\u8f93\u51fa\nDatetimeIndex(['2017-01-05 14:30:00', '2017-01-05 14:30:00',\n                               'NaT', '2017-01-05 00:00:00',\n               '2017-01-05 00:00:00', '2017-01-05 00:00:00'],\n              dtype='datetime64[ns]', freq=None)<\/code><\/pre>\n<\/div>\n

\u56db\u3001\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u65f6\u95f4<\/h2>\n

\u65f6\u95f4\u6233\u4e5f\u662f\u5f88\u5e38\u7528\u7684\u65f6\u95f4\u8868\u73b0\u5f62\u5f0f\uff0c\u4ed6\u9700\u8981\u6307\u5b9a\u4e13\u95e8\u7684\u53c2\u6570unit\u6765\u5904\u7406<\/p>\n

\n
timestamp1 = [1551966534, 1551968279, 1551969279]\nprint(pd.to_datetime(timestamp1, unit='s' ))\ntimestamp2 = [1564733056000, 1564819456000, 1564905856000]\nprint(pd.to_datetime(timestamp2, unit='ms'))<\/code><\/pre>\n<\/div>\n

\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b<\/p>\n

\n
DatetimeIndex(['2019-03-07 13:48:54', '2019-03-07 14:17:59',\n               '2019-03-07 14:34:39'],\n              dtype='datetime64[ns]', freq=None)\nDatetimeIndex(['2019-08-02 08:04:16', '2019-08-03 08:04:16',\n               '2019-08-04 08:04:16'],\n              dtype='datetime64[ns]', freq=None)<\/code><\/pre>\n<\/div>\n

\u4e94\u3001\u7279\u6b8a\u683c\u5f0f\u7684\u5904\u7406<\/h2>\n

\u5728\u4e0e\u5404\u79cd\u4e91\u8ba1\u7b97\u5382\u5546\u6253\u4ea4\u9053\u7684\u65f6\u5019\uff0c\u4f1a\u53d1\u73b0\u4ed6\u4eec\u7684\u65f6\u95f4\u683c\u5f0f\u4e5f\u4e0d\u4e00\u6837\u3002
\n\u6bd4\u5982\u963f\u91cc\u4e91\u4f7f\u75282019-08-02T21:14:00Z\uff0c\u800c\u91d1\u5c71\u4e91\u4f7f\u75282019-08-01T21:14+0800 \uff0c\u6211\u4eec\u6765\u770b\u4e0b\u80fd\u4e0d\u80fd\u8f6c\u6362<\/p>\n

\n
dates = ['2019-08-01T21:14+0800', '2019-07-01 00:00', '2019-08-02T21:14:00Z']\nprint(pd.to_datetime(dates, errors='coerce'))<\/code><\/pre>\n<\/div>\n

\u8fd0\u884c\u7ed3\u679c<\/p>\n

\n
Index([2019-08-01 21:14:00+08:00, 2019-07-01 00:00:00,\n       2019-08-02 21:14:00+00:00],\n      dtype='object')<\/code><\/pre>\n<\/div>\n

\u5f88\u9057\u61be\uff0c\u5f97\u5230\u4e86\u4e00\u4e2aIndex\u7ed3\u679c\uff0c\u800c\u4e0d\u662fDatetimeIndex\u3002
\n\u8fd9\u79cd\u60c5\u51b5\u6211\u4eec\u53ef\u4ee5\u6307\u5b9aformat\u53c2\u6570\u6765\u5b9e\u73b0\u8f6c\u6362\uff0c\u5982\u4e0b\uff0c<\/p>\n

\n
ds1 = ['2019-08-01T21:14+0800', '2019-08-02T21:14+0800', '2019-08-03T21:14+0800']\nprint(pd.to_datetime(ds1, errors='coerce'))\n\nds1 = ['2019-08-01T21:14+0800', '2019-08-02T21:14+0800', '2019-08-03T21:14+0800']\nprint(pd.to_datetime(ds1, errors='coerce', format='%Y-%m-%dT%H:%M+0800'))\n\nds2 = ['2019-08-01T21:14:00Z', '2019-08-02T21:14:00Z', '2019-08-03T21:14:00Z']\nprint(pd.to_datetime(ds2, errors='coerce', format='%Y-%m-%dT%H:%M:%SZ'))<\/code><\/pre>\n<\/div>\n

\u8fd0\u884c\u7ed3\u679c<\/p>\n

\n
DatetimeIndex(['2019-08-01 21:14:00+08:00', '2019-08-02 21:14:00+08:00',\n               '2019-08-03 21:14:00+08:00'],\n              dtype='datetime64[ns, pytz.FixedOffset(480)]', freq=None)\nDatetimeIndex(['2019-08-01 21:14:00', '2019-08-02 21:14:00',\n               '2019-08-03 21:14:00'],\n              dtype='datetime64[ns]', freq=None)\nDatetimeIndex(['2019-08-01 21:14:00', '2019-08-02 21:14:00',\n               '2019-08-03 21:14:00'],\n              dtype='datetime64[ns]', freq=None)<\/code><\/pre>\n<\/div>\n

\u516d\u3001\u5b98\u65b9\u6587\u6863\u94fe\u63a5<\/h2>\n

https:\/\/pandas.pydata.org\/pandas-docs\/stable\/reference\/api\/pandas.to_datetime.html<\/a><\/p>\n

\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1aIPCPU-\u7f51\u7edc\u4e4b\u8def<\/a> » pandas\u4e2d\u7684to_datetime\u51fd\u6570<\/a><\/p>","protected":false},"excerpt":{"rendered":"

\u4e00\u3001\u6982\u8ff0 \u5728\u65e5\u5e38\u6570\u636e\u5904\u7406\u4e2d\uff0c\u6700\u4e3a\u607c\u706b\u7684\u5c31\u662f\u65f6\u95f4\u7684\u5904\u7406\u3002\u65f6\u95f4\u53ef\u4ee5\u6709\u5f88\u591a\u79cd\u8868\u8fbe\u5f62\u5f0f\uff0c\u6bd4\u5982\u4e0b\u9762\u8fd9\u5f20\u56fe\uff0c\u6211\u4eec\u770b\u5230\u540c\u6837\u662f 2017 \u5e74 1 \u6708 5 \u65e5\uff0c\u53ef\u4ee5\u6709\u5f88\u591a\u79cd\u65f6\u95f4\u683c\u5f0f\uff0c\u6211\u4eec\u9700\u8981\u5148\u5c06\u683c\u5f0f\u7edf\u4e00\u624d\u80fd\u8fdb\u884c\u540e\u9762\u7684\u5de5\u4f5c\uff0cPandas \u63d0\u4f9b\u4e86\u8fd9\u4e2a\u51fd\u6570 to_datetime (). \u4e8c\u3001\u7b80\u5355\u7528\u6cd5 dates1 = [‘2017-01-05’, ‘Jan 5, 2017′, ’01\/05\/2017’, ‘2017.01.05’, ‘2017\/01\/05’, ‘20170105’] print(pd.to_datetime(dates1)) dates2 = [‘2017-01-05 2:30:00 PM’, ‘Jan 5, 2017 14:30:00′, ’01\/05\/2016’, ‘2017.01.05’, ‘2017\/01\/05’, ‘20170105’] print(pd.to_datetime(dates2)) \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff0c DatetimeIndex([‘2017-01-05’, ‘2017-01-05’, ‘2017-01-05’, ‘2017-01-05’, ‘2017-01-05’, ‘2017-01-05′], dtype=’datetime64[ns]’, freq=None) DatetimeIndex([‘2017-01-05 14:30:00’, ‘2017-01-05 14:30:00’, ‘2016-01-05 00:00:00’, ‘2017-01-05 00:00:00’, ‘2017-01-05 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,13],"tags":[220,59],"_links":{"self":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1494"}],"collection":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/comments?post=1494"}],"version-history":[{"count":1,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1494\/revisions"}],"predecessor-version":[{"id":1495,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1494\/revisions\/1495"}],"wp:attachment":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/media?parent=1494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/categories?post=1494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/tags?post=1494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}