1

I keep getting these results:

0   2019-02-28 19:24:18.101586+00:00
1   2019-03-01 08:33:15.668978+00:00
[...]
Name: created, dtype: datetime64[ns, UTC]

However, my goal is to get these results:

0   2019-02-28
1   2019-03-01

Do you see what I am doing wrong?

# Load data
def load_ticket_data():
    df = pd.read_csv(
        'wolfsburg/tickets_ticket.csv',
        usecols=['created', 'start_at'],
        parse_dates=['created', 'start_at'],
    )
    return df

tickets = load_ticket_data()
test = pd.to_datetime(tickets['created'], format='%y/%m/%d')
print(test)
Joey Coder
  • 3,199
  • 8
  • 28
  • 60
  • try: `tickets['created'] = tickets['created'].dt.strftime('%y/%m/%d')` – PV8 Jun 25 '19 at 06:28
  • 1
    Possible duplicate of [How to change the datetime format in pandas](https://stackoverflow.com/questions/38067704/how-to-change-the-datetime-format-in-pandas) – PV8 Jun 25 '19 at 06:28
  • @PV8 - no, it is something different. – jezrael Jun 25 '19 at 06:46

1 Answers1

1

If need datetimes with no times use Series.dt.tz_localize with Series.dt.floor:

tickets['created'] = tickets['created'].dt.tz_localize(None).dt.floor('d')
print (tickets)
     created
0 2019-02-28
1 2019-03-01

print (tickets.dtypes)
created    datetime64[ns]
dtype: object
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • The second one gives me the expected result. However `dtype` is changing to an object. Before it was `datetime64`. Is `pd.to_datetime` in general the wrong approach? I thought that's the idea of that method. – Joey Coder Jun 25 '19 at 06:32
  • @JoeyCoder - exactly it means `python object`, I think `dt.floor` is necessary here, it not working for you? – jezrael Jun 25 '19 at 06:33
  • `dt.floor` is working but it makes the "impression" that the event happened at 00:00:00 which would be "wrong" information I give to my model. I will try to work with the `python object`. I just wonder why `pd.to_datetime`doesn't work that way. – Joey Coder Jun 25 '19 at 06:35
  • @JoeyCoder - Tested and you are right, need `'remove'` UTC, answer was edited. – jezrael Jun 25 '19 at 06:39