给定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函数中是否有一种简单的方法可以将Date列转换为Year | Month | Day?
+-------+------+----+----+---------+-----------+
| 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中,我可以这样做:Which is the fastest way to extract day, month and year from a given date?

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

最佳答案

您也可以使用split-datetime方法进行操作。它给您更多的灵活性。

sf.add_columns(sf['Date'].split_datetime(column_name_prefix = ''))
split_datetime方法本身位于SArray(SFrame的单个列)上,它返回一个SFrame,然后您可以将其添加回原始数据(基本上为0)

10-06 13:41