你如何从python日期时间中仅提取日期?

时间:2015-11-20 19:35:55

标签: python datetime pandas tokenize

我在python中有一个数据帧。其中一列标记为<body> <canvas id="canvas" width="800" height="600"></canvas> </body> var canvas, context, xStart, yStart, xEnd, yEnd; canvas = document.getElementById("canvas"); context = canvas.getContext("2d"); canvas.addEventListener("mousedown", mouseDown); canvas.addEventListener("mouseup", mouseUp); function mouseDown(e) { xStart = e.offsetX; yStart = e.offsetY; } function mouseUp(e) { xEnd = e.offsetX; yEnd = e.offsetY; if (xStart != xEnd && yStart != yEnd) { var video = document.createElement("video"); video.src = "http://techslides.com/demos/sample-videos/small.mp4"; video.addEventListener('loadeddata', function() { video.play(); context.drawImage(video, xStart, yStart, xEnd-xStart, yEnd-yStart); }); } } ,这是一个时间戳。使用以下代码,我已将时间戳转换为time

datetime

现在我想分开(标记化)日期和时间,并有两个不同的列,如milestone['datetime'] = milestone.apply(lambda x: datetime.datetime.fromtimestamp(x['time']), axis = 1) milestone['only_date']。我该怎么做?

1 个答案:

答案 0 :(得分:11)

您可以使用date类的timedatetime方法执行此操作:

>>> from datetime import datetime
>>> d = datetime.now()
>>> only_date, only_time = d.date(), d.time()
>>> only_date
datetime.date(2015, 11, 20)
>>> only_time
datetime.time(20, 39, 13, 105773)

Heredatetime文档。

应用于您的示例,它可以提供如下内容:

>>> milestone["only_date"] = [d.date() for d in milestone["datetime"]]
>>> milestone["only_time"] = [d.time() for d in milestone["datetime"]]