Convert Column in Data.Frame to Date

Convert DataFrame column type from string to datetime

The easiest way is to use to_datetime:

df['col'] = pd.to_datetime(df['col'])

It also offers a dayfirst argument for European times (but beware this isn't strict).

Here it is in action:

In [11]: pd.to_datetime(pd.Series(['05/23/2005']))
Out[11]:
0 2005-05-23 00:00:00
dtype: datetime64[ns]

You can pass a specific format:

In [12]: pd.to_datetime(pd.Series(['05/23/2005']), format="%m/%d/%Y")
Out[12]:
0 2005-05-23
dtype: datetime64[ns]

Convert column in data.frame to date

Do the transformations within mutate

df2 %>%
group_by(a1) %>%
mutate(b2=as.Date(b2, format = "%d.%m.%Y"))
# a1 b2 c3 d3
# (chr) (date) (chr) (int)
#1 a 2015-01-01 1a 1
#2 a 2015-02-02 2b 2
#3 b 2012-02-14 3c 3
#4 b 2008-08-16 4d 4
#5 c 2003-06-17 5e 5
#6 d 2015-01-31 6f 6
#7 e 2022-01-07 7g 7
#8 e 2001-05-09 8h 8

If we need to do only the transformation, we don't need to group by 'a1'.

mutate(df2, b2= as.Date(b2, format= "%d.%m.%Y"))

By using %<>% operator from magrittr, we can transform in place.

df2 %<>%
mutate(b2= as.Date(b2, format= "%d.%m.%Y"))


Convert Pandas Column to DateTime

Use the to_datetime function, specifying a format to match your data.

raw_data['Mycol'] =  pd.to_datetime(raw_data['Mycol'], format='%d%b%Y:%H:%M:%S.%f')

Convert String Column directly to Date format (not Datetime) in Pandas DataFrame

pandas.DataFrame.apply is essentially a native python for loop.

pandas.to_datetime is a vectorized function, meaning it's meant to operate on sequences/lists/arrays/series by doing the inner loop in C

If we start with a larger dataframe:

import pandas
df = pandas.DataFrame({'a': ['2020-01-02', '2020-01-02'] * 5000})

And then do (in a jupyter notebook)

%%timeit
df['a'].apply(pandas.to_datetime).dt.date

We get a pretty slow result:

1.03 s ± 48.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

But if we rearrange just slightly to pass the entire column:

%%timeit
pandas.to_datetime(df['a']).dt.date

We get a much faster result:

6.07 ms ± 232 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

How do I convert strings in a Pandas data frame to a 'date' data type?

Use astype

In [31]: df
Out[31]:
a time
0 1 2013-01-01
1 2 2013-01-02
2 3 2013-01-03

In [32]: df['time'] = df['time'].astype('datetime64[ns]')

In [33]: df
Out[33]:
a time
0 1 2013-01-01 00:00:00
1 2 2013-01-02 00:00:00
2 3 2013-01-03 00:00:00

Pandas - Converting date column from dd/mm/yy hh:mm:ss to yyyy-mm-dd hh:mm:ss

If you know you will have a consistent format in your column, you can pass this to to_datetime:

df['sale_date'] = pd.to_datetime(df['sale_date'], format='%d/%m/%y %H:%M:%S')

If your formats aren't necessarily consistent but do have day before month in each case, it may be enough to use dayfirst=True though this is difficult to say without seeing the data:

df['sale_date'] = pd.to_datetime(df['sale_date'], dayfirst=True)

How to change the datetime format in Pandas

You can use dt.strftime if you need to convert datetime to other formats (but note that then dtype of column will be object (string)):

import pandas as pd

df = pd.DataFrame({'DOB': {0: '26/1/2016', 1: '26/1/2016'}})
print (df)
DOB
0 26/1/2016
1 26/1/2016

df['DOB'] = pd.to_datetime(df.DOB)
print (df)
DOB
0 2016-01-26
1 2016-01-26

df['DOB1'] = df['DOB'].dt.strftime('%m/%d/%Y')
print (df)
DOB DOB1
0 2016-01-26 01/26/2016
1 2016-01-26 01/26/2016

How to change data type of column in Data frame to Date from Char

Use

data$Date <- as.Date(data$date, "%m/%d/%Y")

and then to extract month

data$Month <- format(data$Date, "%m")

We can also use lubridate

data$date <- lubridate::mdy(data$date)

and use month to extract the month.

data$month <- month(data$date)

and with anytime

data$Date <- anytime::anydate(data$Date)

How to convert a Date column with lubridate and keep it within the dataframe?

You are overwriting your entire data frame with the value for the newly created date column.

Instead do

dc.crime.complete$Date <- ymd(dc.crime.complete$Date)

This will overwrite your date column with the new values.

R. Convert TimeStamp column from DataFrame to Date Format column

Convert the Timestamp column first to numeric, change it to POSIXct format by passing origin and extract only the date from it.

flight$Flight_Date <- as.Date(as.POSIXct(as.numeric(flight$Timestamp), 
origin='1970-01-01', tz="UTC"))

Example -

as.POSIXct(1643410273, origin='1970-01-01', tz="UTC")
#[1] "2022-01-28 22:51:13 UTC"

as.Date(as.POSIXct(1643410273, origin='1970-01-01', tz="UTC"))
#[1] "2022-01-28"


Related Topics



Leave a reply



Submit