I have a date in format of YYYY-MM-DD (2022-11-01). I want to convert it to 'YYYYMMDD' format (without hyphen). Pls support.
I tried this...
df['ConvertedDate']= df['DateOfBirth'].dt.strftime('%m/%d/%Y')... but no luck
If I understand correctly, the format mask you should be using with strftime is %Y%m%d:
df["ConvertedDate"] = df["DateOfBirth"].dt.strftime('%Y%m%d')
Pandas itself providing the ability to convert strings to datetime in Pandas dataFrame with desire format.
df['ConvertedDate'] = pd.to_datetime(df['DateOfBirth'], format='%Y-%m-%d').dt.strftime('%Y%m%d')
Referenced Example:
import pandas as pd
values = {'DateOfBirth': ['2021-01-14', '2022-11-01', '2022-11-01']}
df = pd.DataFrame(values)
df['ConvertedDate'] = pd.to_datetime(df['DateOfBirth'], format='%Y-%m-%d').dt.strftime('%Y%m%d')
print (df)
Output:
DateOfBirth ConvertedDate
0 2021-01-14 20210114
1 2022-11-01 20221101
2 2022-11-01 20221101
This works
from datetime import datetime
initial = "2022-11-01"
time = datetime.strptime(initial, "%Y-%m-%d")
print(time.strftime("%Y%m%d"))
Related
6 1 I am new to Pandas and Python. I want to do some date time operations in my script. I am getting date time information from a csv file in following format: PT12M20S
How to convert it into pandas datetime format? Something like: 12:20
During Convertion Error is : Unknown string format: PT13M20S
Try this:
pd.to_datetime("PT12M20S", format='PT%HM%MS')
Replace the single string "PT12M20S" with your dataframe column.
EDIT: to reflect additional information given in the comments:
You can use errors="coerce" to ignore all cases where the first format cant be used. And then fill all the missing values using their respective format.
import pandas as pd
df = pd.DataFrame(data=["PT20M23S", "PT45M", "PT25S"], columns=["date"])
df["date"] = pd.to_datetime(df["date"], format="PT%HM%MS", errors="coerce"). \
fillna(pd.to_datetime(df["date"], format="PT%MM", errors="coerce")). \
fillna(pd.to_datetime(df["date"], format="PT%SS", errors="coerce"))
output:
date
0 1900-01-01 20:23:00
1 1900-01-01 00:45:00
2 1900-01-01 00:00:25
Alternatively you can design your own function and apply it.
from datetime import datetime
def format_date(val):
for f in {"PT%HM%MS", "PT%MM", "PT%SS"}:
try:
return datetime.strptime(val, f)
except ValueError:
continue
return None
df["date"].apply(lambda x: format_date(x))
I have a file that contains DateTime in float format
example 14052020175648.000000 I want to convert this into 14-05-2020 and leave the timestamp value.
input ==> 14052020175648.000000
expected output ==> 14-05-2020
Use pd.to_datetime:
df = pd.DataFrame({'Timestamp': ['14052020175648.000000']})
df['Date'] = pd.to_datetime(df['Timestamp'].astype(str).str[:8], format='%d%m%Y')
print(df)
# Output:
Timestamp Date
0 14052020175648.000000 2020-05-14
I used astype(str) in case where Timestamp is a float number and not a string, so it's not mandatory if your column already contains strings.
This can solve your problem
from datetime import datetime
string = "14052020175648.000000"
yourDate = datetime.strptime(string[:8], '%d%m%Y').strftime("%d-%m-%Y")
print(yourDate)
Output:
14-05-2020
I have a date column in a dataset where the dates are like 'Apr-12','Jan-12' format. I would like to change the format to 04-2012,01-2012. I am looking for a function which can do this.
I think I know one guy with the same name. Jokes apart here is the solution to your problem.
We do have an inbuilt function named as strptime(), so it takes up the string and then convert into the format you want.
You need to import datetime first since it is the part of the datetime package of python. Don't no need to install anything, just import it.
Then this works like this: datetime.strptime(your_string, format_you_want)
# You can also do this, from datetime import * (this imports all the functions of datetime)
from datetime import datetime
str = 'Apr-12'
date_object = datetime.strptime(str, '%m-%Y')
print(date_object)
I hope this will work for you. Happy coding :)
You can do following:
import pandas as pd
df = pd.DataFrame({
'date': ['Apr-12', 'Jan-12', 'May-12', 'March-13', 'June-14']
})
pd.to_datetime(df['date'], format='%b-%y')
This will output:
0 2012-04-01
1 2012-01-01
2 2012-05-01
Name: date, dtype: datetime64[ns]
Which means you can update your date column right away:
df['date'] = pd.to_datetime(df['date'], format='%b-%y')
You can chain a couple of pandas methods together to get this the desired output:
df = pd.DataFrame({'date_fmt':['Apr-12','Jan-12']})
df
Input dataframe:
date_fmt
0 Apr-12
1 Jan-12
Use pd.to_datetime chained with .dt date accessor and strftime
pd.to_datetime(df['date_fmt'], format='%b-%y').dt.strftime('%m-%Y')
Output:
0 04-2012
1 01-2012
Name: date_fmt, dtype: object
I have a date column in pandas like the following as string datatype:
df['Date]
Dec/2018
Mar/2017
Sep/2019
I want the date column in pandas like the following as datetime datatype,
not string datatype:
df['Date]
`December-2018`
`March-2017`
`September-2019`
from datetime import datetime
import pandas as pd
# define sample data to create a dataframe from
data = {'Date': ['Dec/2018', 'Mar/2017', 'Sep/2019']}
df = pd.DataFrame(data)
# define a function to convert dates
def format_date(el):
return datetime.strptime(el, '%b/%Y').strftime('%B-%Y')
# apply conversion to desired column and store output in new column
df['Date_formatted'] = df['Date'].apply(format_date)
# print the dataframe to check result
print(df)
Which will output the dataframe as this:
Date Date_formatted
0 Dec/2018 December-2018
1 Mar/2017 March-2017
2 Sep/2019 September-2019
The pandas approach using pd.to_datetime:
df['Date'] = pd.to_datetime(df['Date'], format='%b/%Y').dt.strftime('%B-%Y')
0 December-2018
1 March-2017
2 September-2019
Name: Date, dtype: object
import datetime
x = datetime.date.today()
print(x.strftime("%B - %Y "))
For additional information, you can read this.
I hope it will help you.
I am reading from an Excel sheet. The header is date in the format of Month-Year and I want to keep it that way. But when it reades it, it changes the format to "2014-01-01 00:00:00". I wrote the following peice to fix it, but doesn't work.
import pandas as pd
import numpy as np
import datetime
from datetime import date
import time
file_loc = "path.xlsx"
df = pd.read_excel(file_loc, index_col=None, na_values=['NA'], parse_cols = 37)
df.columns=pd.to_datetime(df.columns, format='%b-%y')
Which didn't do anything. On another try, I did the following:
df.columns = datetime.datetime.strptime(df.columns, '%Y-%m-%d %H:%M:%S').strftime('%b-%y')
Which returns the must be str, not datetime.datetime error. I don't know how make it read the row cell by cell to read the strings!
Here is a sample data:
NaT 11/14/2015 00:00:00 12/15/2015 00:00:00 1/15/2016 00:00:00
A 5 1 6
B 6 3 3
My main problem with this is that it does not recognize it as the header, e.g., df['11/14/2015 00:00:00'] retuns an keyError.
Any help is appreciated.
UPDATE: Here is a photo to illustrate what I keep geting! Box 6 is the implementation of apply, and box 7 is what my data looks like.
import datetime
df = pd.DataFrame({'data': ["11/14/2015 00:00:00", "11/14/2015 00:10:00", "11/14/2015 00:20:00"]})
df["data"].apply(lambda x: datetime.datetime.strptime(x, '%m/%d/%Y %H:%M:%S').strftime('%b-%y'))
EDIT
If you'd like to work with df.columns you could use map function:
df.columns = list(map(lambda x: datetime.datetime.strptime(x, '%m/%d/%Y %H:%M:%S').strftime('%b-%y'), df1.columns))
You need list if you are using python 3.x because it's iterator by default.
The problem might be that the data in excel isn't stored in the string format you think it is. Perhaps it is stored as a number, and just displayed as a date string in excel.
Excel sometimes uses milliseconds after an epoch to store dates.
Check what the actual values you see in the df array.
What does this show?
from pprint import pprint
pprint(df)