0

I have this dataframe, sales and the column, dates. enter image description here

enter image description here

When I try to see the range of dates, it shows me them like this. It shows the last date, 31.10.2015, somewhere in the middle instead of the end of this array.

In: sales.date.unique()
Out: array(['01.01.2013', '01.01.2014', '01.01.2015', ..., '31.10.2015',
       '31.12.2013', '31.12.2014'], dtype=object)

when I have tried to sort these dates with

sales.date.sort_values(), it still gives me '31.12.2014' as the last date.

then, I converted my dates to datetime64[ns] with

sales["date"] = pd.to_datetime(sales["date"],format = "%d.%m.%Y")
sales["date"] = pd.to_datetime(sales["date"],format = "%d.%m.%Y")
dates = sales.date.sort_values()

dates.unique()

I got this format

array(['2013-01-01T00:00:00.000000000', '2013-01-02T00:00:00.000000000',
       '2013-01-03T00:00:00.000000000', ...,
       '2015-10-29T00:00:00.000000000', '2015-10-30T00:00:00.000000000',
       '2015-10-31T00:00:00.000000000'], dtype='datetime64[ns]')

How can I remove the 'T00:00:00.000000000' part? I have specified format = "%d.%m.%Y" in pd_todatetime..

sorry if that's somewhat a basic question. thank you for your help.

Bluetail
  • 1,093
  • 2
  • 13
  • 27
  • 2
    `df['DOB1'] = df['DOB'].dt.strftime('%m/%d/%Y')` or https://stackoverflow.com/questions/38067704/how-to-change-the-datetime-format-in-pandas – Paul Brennan Dec 31 '20 at 15:48
  • 2
    The format you specified in `to_datetime` is for **reading** the string data to convert them to the internal datetime format (datetime64[ns]) used by pandas. What you see in the output of `dates.unique()` is just the standard string representation of the datetime64 values of this array. – Stef Dec 31 '20 at 16:18
  • this has helped me, thanks guys! not sure how to vote for 'solved my problem'. – Bluetail Dec 31 '20 at 19:01

1 Answers1

0
sales['date'] = sales['date'].dt.strftime('%Y/%d/%m')
dates = sales.date.sort_values()
dates.unique()

Out:
array(['2013/01/01', '2013/01/02', '2013/01/03', ..., '2015/31/07','2015/31/08', '2015/31/10'], dtype=object)

thank you for the comments above!

Bluetail
  • 1,093
  • 2
  • 13
  • 27