2

I have a column called date with this values

> DatetimeIndex(['2014-02-19'], dtype='datetime64[ns]', freq=None)
> DatetimeIndex(['2013-02-29'], dtype='datetime64[ns]', freq=None)
> DatetimeIndex(['2018-04-15'], dtype='datetime64[ns]', freq=None)

how do i modify the column to just extract the date values and get rid of words like DatetimeIndex and brackets etc?

> 2014-02-19
> 2013-02-19
> 2018-04-15
 

The code I wrote I think is pretty incorrect but still attaching it here:

def fundate(x):
    return x[0][0]

df['date'] = df.apply(lambda row : fundate(row['date']), axis = 1)

could someone please help me?

  • This should work https://stackoverflow.com/questions/52278464/convert-datetimeindex-to-datetime-date-in-pandas – SunilG May 28 '21 at 07:15

5 Answers5

0

Do you mean something like this (not tested)?

def fundate(x):
    return x.strftime("%Y-%m-%d")

From https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.strftime.html

umbe1987
  • 2,894
  • 6
  • 35
  • 63
0

I think this is what you want. It selects the first item from the index and changes it to a string.

df['new_column'] = df['date'].apply(lambda x: x[0].strftime("%Y-%m-%d"))
Rutger
  • 593
  • 5
  • 11
0

If there is object DatetimeIndex use:

df['date'] = df['date'].str[0]

If there is multiple values per row:

df = df.explode('date')

If there are strings and only one value per row:

df["date"] = df["date"].str.extract(r"(\d{4}-\d{2}-\d{2})", expand=False)

If possible multipel values per row:

df["date"] = df["date"].str.findall(r"(\d{4}-\d{2}-\d{2})")
df = df.explode('date')
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

We are using regex to fetch date element between square brackets of input string.

If you need output in object/str

df.date = df["date"].str.extract('\[(.*?)]', expand=True).replace("'","", regex=True)

Output

    date
0   2014-02-19
1   2013-02-28
2   2018-04-15

Output Type

df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 1 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   date    3 non-null      object
dtypes: object(1)
memory usage: 152.0+ bytes

If output is required as datetime type

df.date = df["date"].str.extract('\[(.*?)]', expand=True).replace("'","", regex=True)
df.date = pd.to_datetime(df.date)
df

Output

    date
0   2014-02-19
1   2013-02-28
2   2018-04-15

Output Type

df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 1 columns):
 #   Column  Non-Null Count  Dtype         
---  ------  --------------  -----         
 0   date    3 non-null      datetime64[ns]
dtypes: datetime64[ns](1)
memory usage: 152.0 bytes

Input DF

df = pd.DataFrame({
    'date':["DatetimeIndex(['2014-02-19'], dtype='datetime64[ns]', freq=None)",
"DatetimeIndex(['2013-02-28'], dtype='datetime64[ns]', freq=None)",
"DatetimeIndex(['2018-04-15'], dtype='datetime64[ns]', freq=None)"]
})

df
Utsav
  • 5,572
  • 2
  • 29
  • 43
0

You can do:

import pandas as pd
import datetime as dt
rng = pd.date_range('2015-02-24', periods=5, freq='T')
df = pd.DataFrame({ 'date': rng })
    
df['date'] = df['date'].dt.strftime("%Y-%m-%d")

to convert a pd.Series with dtype datetime64ns to a string

If you like to convert that pd.Series to an list you can do:

datelist = df['date'].tolist()
peet_overflow
  • 11
  • 1
  • 2