5

给定一个graphlab SFrame带有日期的列,例如:

+-------+------------+---------+-----------+
| Store |    Date    |  Sales  | Customers |
+-------+------------+---------+-----------+
|   1   | 2015-07-31 |  5263.0 |   555.0   |
|   2   | 2015-07-31 |  6064.0 |   625.0   |
|   3   | 2015-07-31 |  8314.0 |   821.0   |
|   4   | 2015-07-31 | 13995.0 |   1498.0  |
|   3   | 2015-07-20 |  4822.0 |   559.0   |
|   2   | 2015-07-10 |  5651.0 |   589.0   |
|   4   | 2015-07-11 | 15344.0 |   1414.0  |
|   5   | 2015-07-23 |  8492.0 |   833.0   |
|   2   | 2015-07-19 |  8565.0 |   687.0   |
|   10  | 2015-07-09 |  7185.0 |   681.0   |
+-------+------------+---------+-----------+
[986159 rows x 4 columns]

在graphlab /其他python函数中是否有一种简单的方法可以将日期列转换为年|月|日?

+-------+------+----+----+---------+-----------+
| Store | YYYY | MM | DD |  Sales  | Customers |
+-------+------+----+----+---------+-----------+
|   1   | 2015 | 07 | 31 |  5263.0 |   555.0   |
|   2   | 2015 | 07 | 31 |  6064.0 |   625.0   |
|   3   | 2015 | 07 | 31 |  8314.0 |   821.0   |
+-------+------------+---------+-----------+
[986159 rows x 4 columns]

pandas中,我可以这样做:从给定日期提取日、月和年的最快方法是什么?

但是将 SFrame 转换为 Panda 以拆分日期并转换回 SFrame 是一件很麻烦的事。

4

2 回答 2

4

You could also do it with the split-datetime method. It gives you a bit more flexibility.

sf.add_columns(sf['Date'].split_datetime(column_name_prefix = ''))

The split_datetime method itself is on the SArray (a single column of the SFrame) and it returns an SFrame which you can then add back to the original data (at basically 0 cost)

于 2015-11-19T08:04:57.277 回答
2

一种快速而肮脏的方法是

sf['date2'] = sf['Date'].apply(lambda x: x.split('-'))
sf = sf.unpack('date2')

另一种选择是将Date列转换为日期时间类型,然后使用该graphlab.SArray.split_datetime函数。

于 2015-11-19T00:27:05.620 回答