pandas 将熊猫日期时间列 yyyy-mm-dd 转换为 YYYMMDD

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46834732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:40:05  来源:igfitidea点击:

convert pandas datetime column yyyy-mm-dd to YYYMMDD

pythonpandasdatetime

提问by user3757265

I have a dateframe with datetime column in the format yyyy-mm-dd.

我有一个日期时间列的日期框,格式为 yyyy-mm-dd。

I would like to have it in interger format yyyymmdd . I keep throwing an error using this

我想以整数格式 yyyymmdd 使用它。我一直在用这个抛出错误

x=dates.apply(dt.datetime.strftime('%Y%m%d')).astype(int)

TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'str'

This doesn't not work as i tried to pass an array. I know that if I pass just on element it will convert, but how do I do it more pythonic? I did try using lambda but that didn't work either.

这不起作用,因为我试图传递一个数组。我知道如果我只传递元素它会转换,但我如何做更pythonic?我确实尝试使用 lambda 但这也不起作用。

回答by Scott Boston

If your column is a string, you will need to first use `pd.to_datetime',

如果您的列是字符串,则需要先使用“pd.to_datetime”,

df['Date'] = pd.to_datetime(df['Date'])

Then, use .dtdatetime accessor with strftime:

然后,使用.dt日期时间访问器strftime

df = pd.DataFrame({'Date':pd.date_range('2017-01-01', periods = 60, freq='D')})

df.Date.dt.strftime('%Y%m%d').astype(int)

Or use lambda function:

或者使用 lambda 函数:

df.Date.apply(lambda x: x.strftime('%Y%m%d')).astype(int)

Output:

输出:

0     20170101
1     20170102
2     20170103
3     20170104
4     20170105
5     20170106
6     20170107
7     20170108
8     20170109
9     20170110
10    20170111
11    20170112
12    20170113
13    20170114
14    20170115
15    20170116
16    20170117
17    20170118
18    20170119
19    20170120
20    20170121
21    20170122
22    20170123
23    20170124
24    20170125
25    20170126
26    20170127
27    20170128
28    20170129
29    20170130
30    20170131
31    20170201
32    20170202
33    20170203
34    20170204
35    20170205
36    20170206
37    20170207
38    20170208
39    20170209
40    20170210
41    20170211
42    20170212
43    20170213
44    20170214
45    20170215
46    20170216
47    20170217
48    20170218
49    20170219
50    20170220
51    20170221
52    20170222
53    20170223
54    20170224
55    20170225
56    20170226
57    20170227
58    20170228
59    20170301
Name: Date, dtype: int32

回答by YOBEN_S

You know what? I have a dirty way

你知道吗?我有一个肮脏的方式

df.Date.dt.year*10000+df.Date.dt.month*100+df.Date.dt.day
Out[498]: 
0     20170101
1     20170102
2     20170103
3     20170104
4     20170105
5     20170106
6     20170107
7     20170108
8     20170109
9     20170110
10    20170111
11    20170112
12    20170113
13    20170114
14    20170115
15    20170116
16    20170117
17    20170118
18    20170119
19    20170120
20    20170121
21    20170122
22    20170123
23    20170124
24    20170125
25    20170126
26    20170127
27    20170128
28    20170129
29    20170130
30    20170131
31    20170201
32    20170202
33    20170203
34    20170204
35    20170205
36    20170206
37    20170207
38    20170208
39    20170209
40    20170210
41    20170211
42    20170212
43    20170213
44    20170214
45    20170215
46    20170216
47    20170217
48    20170218
49    20170219
50    20170220
51    20170221
52    20170222
53    20170223
54    20170224
55    20170225
56    20170226
57    20170227
58    20170228
59    20170301
Name: Date, dtype: int64