0

Create a dataframe whose first column is a text.

import pandas as pd
values = {'dates':  ['2019','2020','2021'],
          'price': [11,12,13]
          }
df = pd.DataFrame(values, columns = ['dates','price'])

Check the dtypes:

df.dtypes
dates    object
price     int64
dtype: object

Convert type in the column dates to type dates.

df['dates'] = pd.to_datetime(df['dates'], format='%Y')
df
       dates  price
0 2019-01-01     11
1 2020-01-01     12
2 2021-01-01     13

I want to convert the type in dates column to date and the dates in the following format----contains only year number:

    dates  price
0 2019     11
1 2020     12
2 2021     13

How can achieve the target?

showkey
  • 482
  • 42
  • 140
  • 295
  • There is not a a date type which is only the year. Minimally dates need a year, month and day. If an element is missing it will be provided. Typically the default missing day and month is the first. What are you trying to do where an `int` value does not work? Or what are you trying to do that the date being the first of the year does not work? – Henry Ecker Aug 13 '21 at 03:47
  • 1
    @AnuragDabas I was providing a (I hope meaningful) answer, can you reopen? – mozway Aug 13 '21 at 03:49

1 Answers1

1

If you choose to have the datetime format for your columns, it is likely to benefit from it. What you see in the column ("2019-01-01") is a representation of the datetime object. The realquestion here is, why do you need to have a datetime object?

Actually, I don't care about datetime type:

Use a string ('2019'), or preferentially an integer (2019) which will enable you to perform sorting, calculations, etc.

I need the datetime type but I really want to see only the year:

Use style to format your column while retaining the underlying type:

df.style.format({'dates': lambda t: t.strftime('%Y')})

This will allow you to keep the type while having a clean visual format

mozway
  • 194,879
  • 13
  • 39
  • 75
  • 1
    Noteworthy that the pandas Styler does not work outside of interactive HTML environments like Juypter notebook, and can only be exported to environments which support its formatting like excel, and LaTeX. There is a very similar answer to this one in [How to change the datetime format in Pandas](https://stackoverflow.com/a/60273382/15497888) – Henry Ecker Aug 13 '21 at 03:56