Search code examples
python-3.xpython-datetime

Transform a long format date to a short format


The small program I am using (below) gives a Date0 value in a "long" form:

import pandas as pd
from datetime import datetime, timedelta

df = pd.DataFrame(
         [
             ["A",  -33.415424]
         ],
         columns = [
             "Country",
             "Time0"
         ]
    )
df = df.set_index("Country", drop = True)
d = datetime.strptime("2020-01-22", '%Y-%m-%d')
df['d'] = d
df['Date0'] = df['d'] +  pd.to_timedelta(df['Time0'], unit='d')
              Time0             d                        Date0
Country             
A        -33.415424     2020-01-22  2019-12-19 14:01:47.366400

How can I get Date0 to be "only" 2019-12-19?

Surely a basic question, but I'm sorry to say that I am completely lost in formatting dates in Python...


Solution

  • you can do it in this way:

    df['Date0'] = df['Date0'].dt.date
    

    Output:

               Time0         d        Date0
      Country           
         A   -33.415424 2020-01-22  2019-12-19