简体   繁体   English

python 变换时间戳

[英]python transform time stamp

Is it possible with pandas to transform a column Date that looks like this: (I think ISO format without the T )是否可以使用 pandas 转换如下所示的列Date :(我认为没有T的 ISO 格式)

2020-06-29 14:04:21.000000
2020-06-29 14:19:26.000000
2020-06-29 14:34:30.000000
2020-06-29 14:49:35.000000
2020-06-29 15:04:39.000000
2020-06-29 15:19:44.000000
2020-06-29 15:34:48.000000
2020-06-29 15:49:53.000000
2020-06-29 16:20:02.000000
2020-06-29 16:35:07.000000
2020-06-29 16:50:11.000000
2020-06-29 17:05:16.000000
2020-06-29 17:20:20.000000
2020-06-29 17:35:25.000000
2020-06-29 17:50:30.000000
2020-06-29 18:05:34.000000
2020-06-29 18:20:39.000000
2020-06-29 18:35:43.000000
2020-06-29 18:50:48.000000
2020-06-29 19:05:52.000000

To a format like this with only hours & minutes.像这样只有小时和分钟的格式。 Rounding to closest minute would be good enough.四舍五入到最接近的分钟就足够了。 Can I go right from time.time.now() to a format that looks this below?我可以从time.time.now() go 到如下所示的格式吗? Thanks for any tips...感谢您的任何提示...

1/19/2019 0:30
1/19/2019 0:45
1/19/2019 1:00
1/19/2019 1:15
1/19/2019 1:30
1/19/2019 1:45
1/19/2019 2:00
1/19/2019 2:15
1/19/2019 2:30
1/19/2019 2:45
1/19/2019 3:00
1/19/2019 3:15
1/19/2019 3:30
1/19/2019 3:45
import pandas as pd

data = {'Date': ['2019-05-30 00:00:44.000000', '2019-05-30 00:00:50.000000','2019-05-30 00:23:26.000000']}
df = pd.DataFrame(data=data)

df['New_Date'] = pd.to_datetime(df['Date']).dt.round('min').dt.strftime('%m/%d/%Y %H:%M')

Output: Output:

                  Date          New_Date
0  2019-05-30 00:00:44  05/30/2019 00:01
1  2019-05-30 00:00:50  05/30/2019 00:01
2  2019-05-30 00:23:26  05/30/2019 00:23

If you are looking to go from the time.time() method to the desired format you can use this:如果您正在寻找 go 从time.time()方法到所需的格式,您可以使用这个:

time.strftime('%m/%d/%Y %H:%M')

Furthermore if your pandas variable is a valid Datetime object you can apply strftime to it.此外,如果您的 pandas 变量是有效的日期时间 object 您可以对其应用strftime

def formatTime(origTD):
    date = origTD.split(' ')[0]
    time = origTD.split(' ')[1]

    day = int(date.split('-')[2])
    month = int(date.split('-')[1])
    year = date.split('-')[0][-2:]

    hour = time.split(':')[0]
    minute = time.split(':')[1]

    newTD = str(month)+'/'+str(day)+'/'+year+' '+hour+':'+minute

    return newTD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM