【发布时间】:2018-05-11 07:45:21
【问题描述】:
需要以纪元格式获取当前月份的范围(从开始到月末,两个变量,纪元格式,以毫秒为单位)。怎么做?也许有人已经这样做了并且可以提供帮助?
【问题讨论】:
标签: python pandas datetime time
需要以纪元格式获取当前月份的范围(从开始到月末,两个变量,纪元格式,以毫秒为单位)。怎么做?也许有人已经这样做了并且可以提供帮助?
【问题讨论】:
标签: python pandas datetime time
不是熊猫,而是一般:
from datetime import datetime
import calendar
epoch = datetime.utcfromtimestamp(0)
def unix_time(dt):
return (dt - epoch).total_seconds() * 1000.0
today = datetime.now()
lastDay = calendar.monthrange(today.year, today.month)[1]
firstDayOfMonth = today.replace(day=1)
lastDayofMonth = today.replace(day=lastDay)
firstEpoch = unix_time(firstDayOfMonth)
lastEpoch = unix_time(lastDayOfMonth)
【讨论】:
来自 Pandas 文档:From timestamp to epoch。将 datetime 转换为 int64,然后除以转换单位。
import pandas as pd
def to_epoch(**kwargs):
"""returns: NumPy ndarray."""
stamps = pd.date_range(**kwargs)
return stamps.view('int64') // pd.Timedelta(1, unit='s')
to_epoch(start='2017-04-01', end='2017-04-30')
array([1491004800, 1491091200, 1491177600, 1491264000, 1491350400,
1491436800, 1491523200, 1491609600, 1491696000, 1491782400,
1491868800, 1491955200, 1492041600, 1492128000, 1492214400,
1492300800, 1492387200, 1492473600, 1492560000, 1492646400,
1492732800, 1492819200, 1492905600, 1492992000, 1493078400,
1493164800, 1493251200, 1493337600, 1493424000, 1493510400])
【讨论】: