This question already has answers here:
How to change the datetime format in Pandas
(8 answers)
Closed last month.
I have a DataFrame with a str column of dates like
'time': '2018-02-25T08:00:00.000Z'
but when applying pd.to_datetime to that column, the output looks like
'time': '2018-02-25 08:00:00+00:00'
Why is the format changing? I tried
pd.to_datetime(utc = True)
and
pd.to_datetime(..., format='%Y-%m-%dT%H:%M:%S.%f%z')
but none of them seem to work and the format gets changed.
Following #roganjosh comments, I converted it back to string with
df['time'].dt.strftime('%Y-%m-%dT%H:%M:%S.000Z')
Related
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()
This question already has answers here:
Convert string into datetime.time object
(4 answers)
Closed 1 year ago.
I have a string in the str format
PATTERN_OUT = "%H:%M"
date_time = (datetime.strftime(enddateandtime, PATTERN_OUT))
I need to convert it to datetime.time. How can this be done?
You can utilize the fact that time string is \d\d:\d\d.
Look at the following snippet.
from datetime import time
time_str = "10:01"
time(*map(int, time_str.split(':')))
Add exception handler if required.
This question already has answers here:
datetime to string with series in pandas
(3 answers)
Closed 2 years ago.
I'm converting a datetime column (referred to as DATE) in my Pandas dataframe df to a string of the form 'Ymd' (e.g. '20191201' for December 1st 2019). My current way of doing that is:
import datetime as dt
df['DATE'] = df['DATE'].apply(lambda x: dt.datetime.strftime(x, '%Y%m%d'))
But this is surprisingly inefficient and slow when run on large dataframes with millions of rows. Is there a more efficient alternative I am not seeing? That would be extremely helpful. Thanks.
In pandas you do not need apply
df['Date']=df['DATE'].dt.strftime('%Y%m%d')
This question already has answers here:
Extracting just Month and Year separately from Pandas Datetime column
(13 answers)
Closed 3 years ago.
I have a dataframe with dates, and I want to make a column with only the month of the corresponding date in each row. First, I converted my dates to ts objects like this:
df['Date'] = pd.to_datetime(df['Date'])
After that, I tried to make my new column for the month like this:
df['Month'] = df['Date'].month
However, it gives me an error:
AttributeError: 'Series' object has no attribute 'month'
I do not understand why I can't do it like this. I double checked whether the conversion to ts objects actually works, and that does work. Also, if I extract 1 date using slicing, I can append .month to get the month. I technically could solve the problem by looping over all indices and then slicing for each index, but my dataframe contains 166000+ rows so that is not an option.
You have to use property (or accessor object) dt
df["month"] = df.date.dt.month
This question already has an answer here:
Python converting datetime to be used in os.utime
(1 answer)
Closed 6 years ago.
I'm trying to convert an ordinal dateTime value into a value that can be accepted by os.utime()
My date has the format: 201642322295
(which is: year=2016, month=4, day=23, hour=22, minute=29, second=5)
However, the method won't accept this and I'm not sure how to/ what to convert it into. I've tried converting it into a datetime also but this does not work.
The code segment:
s = int(self.newTime)
date = datetime(year=s[0:4], month=s[4], day=s[5:2], hour=s[8:2], minute=s[10:2], second=s[12])
os.utime(self.fileName, (date,date))
(I had tried using just the ordinal format, which modifies the datetime of the file but is not at all correct)
edit: This is not the same as 'python converting datetime to be used in os.utime' because it's using a completely different format
os.utime expects values in the form of integers representing a untix timestamp.
s = int(self.newTime)
date = datetime(year=s[0:4], month=s[4], day=s[5:2], hour=s[8:2], minute=s[10:2], second=s[12])
utime = time.mktime(date.timetuple())
os.utime(self.fileName, (utime, utime))