How to add - to the cell that has dates [duplicate] - python

This question already has answers here:
How to change the datetime format in Pandas
(8 answers)
How can I format date time string which is not date time format so that I can use it with pd.to_datetime()?
(2 answers)
Convert DataFrame column type from string to datetime
(6 answers)
Closed 5 months ago.
I have a 'date' column that has a date value as 20170423 (yyyymmdd) how can i change it to 2017-04-23?
dataframe = df
column name 'date'
I read the below post, but none of the solutions worked
Fastest way to insert these dashes in python string?
Example
d = df['Date']
df['Date'] = '%s-%s-%s' % (d[:4], d[4:6], d[6:])
The output 0 0 20170711\n1 20170718\n2 20170718\n3... when i exported in .csv

Related

CONVERT Date column to %m-%d-%y [duplicate]

This question already has answers here:
Convert Pandas Column to DateTime
(8 answers)
How to change the datetime format in Pandas
(8 answers)
Closed last month.
I want to convert the date column in my dataframe which is in the format of Dec. 01, 2022 to 12-01-2022
I tried using map function and by importing calender as well

changing date format of column in pyspark [duplicate]

This question already has answers here:
Convert pyspark string to date format
(6 answers)
Closed 5 months ago.
I have a column of dates in a pyspark dataframe in the format 01-01-1999 (dd-mm-yyyy)and I would like to change the date format of the entire column to 1999-01-01 (yyyy-mm-dd). Any help?
In pyspark we have this function:
date_format(dateExpr,format)
It converts a date/timestamp/string to a value of string in the format specified by the date format given by the second argument.
You can try ths one:
df.select(date_format('your_column_date', "yyyy-MM-dd")).show()

How do I remove hours and seconds from my DataFrame column in python? [duplicate]

This question already has answers here:
How to change the datetime format in Pandas
(8 answers)
Keep only date part when using pandas.to_datetime
(13 answers)
Closed 10 months ago.
I have a DataFrame :
Age Gender Address Date
15 M 172 ST 2022-02-07 00:00:00
I Want to remove hh:mm:ss
I tried:
import datetime as dt
df["Date"]=df["Date"].dt.Date .
But I am receiving no change in date column format.
All I want is that the date column has only (YYYY-MM-DD).
You can use pd.to_datetime to convert Date column to datetime object.
df['Date'] = pd.to_datetime(df['Date']).dt.date
# or
df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%Y-%m-%d')
# or
df['Date'] = df['Date'].str.split(' ').str[0]

Change the date format to its respective month and year only in python [duplicate]

This question already has answers here:
How to convert a date string to different format [duplicate]
(2 answers)
Closed 1 year ago.
I have a date say - 25-Jan-19
I want to convert it to - Jan19 in python. What date format I'll have to use to get this?
If 25-Jan-19 is a string and will always be in this format, you can try this:-
date = date.split("-")
date = "".join(date[i] for i in range(1,len(date)))

Python convert integer to date [duplicate]

This question already has answers here:
How to convert a given ordinal number (from Excel) to a date
(5 answers)
Closed 7 years ago.
I have this list of integers and need to convert it to date using python.
42222 should be 8/6/2015
42290 should be 10/13/2015
42319 should be 11/11/2015
I get the equal date of the integer when i paste in to excel then format the cell to Date.
Excel dates start counting around the year 1900. This will do it:
from datetime import datetime, timedelta
def xldate_to_datetime(xldate):
tempDate = datetime(1900, 1, 1)
deltaDays =timedelta(days=int(xldate)-2)
TheTime = (tempDate + deltaDays )
return TheTime.strftime("%m/%d/%Y")
>>> xldate_to_datetime(42290)
'10/13/2015'

Categories