简体   繁体   English

如何在python中将日期从YYYY-MM-DD更改为YYDD-MM-YY

[英]How to change the date from YYYY-MM-DD to YYDD-MM-YY in python

I have a MySQL database, and there is a column in which I have stored the date.我有一个 MySQL 数据库,并且有一列存储了日期。 Now the requirement is I need to replace the dd with YY.现在的要求是我需要用 YY 替换 dd。 and I want to do this using python(Pandas)我想使用 python(Pandas) 来做到这一点

for eg date that i have is 2032-11-16, and I want 2016-11-32.例如,我的日期是 2032-11-16,我想要 2016-11-32。

uptill now my approach is shown below直到现在我的方法如下所示

df = pd.read_csv('p1006_freight_rates.csv')
df['year'] = zip(*df['date'].apply(lambda x: (x[:4], x[5:])))

the above approach is absolutely wrong, I have also tried the string replace but that too had some issue.上面的方法是绝对错误的,我也尝试过字符串替换,但也有一些问题。

and also please tell me how to save the update in mysql.还请告诉我如何在 mysql 中保存更新。

datetime module could help you with that: datetime模块可以帮助您:

datetime.datetime.strptime(date_string, format1).strftime(format2)

datetime.datetime.strptime("2013-1-25", '%Y-%m-%d').strftime('%m/%d/%y')

prints "01/25/13" .打印"01/25/13"

If you can't live with the leading zero, try this:如果你不能忍受前导零,试试这个:

dt = datetime.datetime.strptime("2013-1-25", '%Y-%m-%d')
print '{0}/{1}/{2:02}'.format(dt.month, dt.day, dt.year % 100)

This prints "1/25/13" .这将打印"1/25/13"

EDIT: This may not work on every platform:编辑:这可能不适用于每个平台:

datetime.datetime.strptime("2013-1-25", '%Y-%m-%d').strftime('%-m/%d/%y')

A simple and straight forward method would be like this:一个简单而直接的方法是这样的:

xx = [
    '2032-11-16',
    '2011-22-33'
]
df = pd.DataFrame(xx,columns=['date'])
df['new_date'] = df.date.map(lambda x:x[:2]+x[-2:]+x[4:8]+x[2:4])
df

Well, if you have a date string that you KNOW is in the format YYDD-MM-YY, then just use this:好吧,如果您知道日期字符串的格式为 YYDD-MM-YY,那么只需使用以下命令:

new_str = old_str;
new_str[2:4] = old_str[8:10]
new_str[8:10] = old_str[2:4]

暂无
暂无

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

相关问题 如何在 Python 中将日期从“yyyy-mm-dd”更改为“mm/dd/yyyy”? - How to change date from “yyyy-mm-dd” to “mm/dd/yyyy” in Python? 如何将 csv 文件中的时间格式从 DD:MM:YY HH:MM 更改为 YYYY-MM-DD HH:MM:SS。 或 YYYY/MM/DD HH:MM:SS - How to change time format in a csv file from DD:MM:YY HH:MM to YYYY-MM-DD HH:MM:SS. or YYYY/MM/DD HH:MM:SS 将python中索引的日期格式从yyyy-dd-mm更改为yyyy-mm-dd - Change date format for an index in python from yyyy-dd-mm to yyyy-mm-dd 对于1970年之前的日期,Python将日期格式从dd / mm / yy更改为dd / mm / yyyy - Python change the date format from dd/mm/yy to dd/mm/yyyy for the dates before 1970 日期从YYYY-MM-DD转换为CSV的一半,并且在切换日期时间后不再起作用 - Date change halfway through csv from YYYY-MM-DD to DD/MM/YY and after switch datetime no longer works Python脚本将Excel日期格式从mmm-yy转换为yyyy-mm-dd? - Python Script to convert Excel Date Format from mmm-yy to yyyy-mm-dd? 如何更改日期格式(从 yyyy-MM-DD 到 yyyy-MM) - How to change date format (from yyyy-MM-DD to yyyy-MM) (Python) 将日期格式从 yyyy-mm-dd 更改为 yyyy/mm/dd - (Python) changing date format from from yyyy-mm-dd to yyyy/mm/dd 使用 python 将日期格式 mm/dd/yyyy hh:mm:ss AM 更改为 yyyy-mm-dd hh:mm:ss 格式 - change date format mm/dd/yyyy hh:mm:ss AM to yyyy-mm-dd hh:mm:ss format using python 如何在 Python 中将日期格式 (YYYY-MM-DD) 转换为 (YYYY,MM,DD) - How to convert a date format (YYYY-MM-DD) to (YYYY,MM,DD) in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM