-1

Using the to_datetime function I am getting the dates as well as the time when i am print it out. I only need the dates. Is there any method to remove it while converting?

pd.to_datetime(pd.Series(dates))

  • 0 2013-12-20 00:00:00
  • 1 2013-12-21 00:00:00
  • 2 2013-12-22 00:00:00
  • 3 2013-12-23 00:00:00

the code:(My data don't have time in it but its printing the time. Also I would like to add that its printing time while I am printing it after doing groupby on it)

    data = pd.read_csv('workingfile.csv')
    data['WEEK'] = pd.to_datetime(data['WEEK'],dayfirst = True)
    grouped = data.groupby(['UPC','WEEK'])
    for (upc,week), group in grouped:
        print week

    WEEK
    02/06/2013 00:00
    09/06/2013 00:00
    16/06/2013 00:00
    23/06/2013 00:00
    30/06/2013 00:00
    07/07/2013 00:00
    14/07/2013 00:00
jithinvr
  • 11
  • 3
  • You can just access the `date` attribute like so: `df['date'] = df['datetime'].apply(lambda x: x.date())` – EdChum Sep 26 '14 at 11:21
  • Also it's important to note that the display only displays if you have time data in the first place, if your string had time in the first place then this will be converted and displayed in the output unless you specifically take just the date attribute. This has to be done after the conversion. – EdChum Sep 26 '14 at 11:24
  • On my system, after the `to_datetime` conversion your data is displayed without the time so I don't know what is wrong at your end. Can you post code, raw data and the output – EdChum Sep 26 '14 at 11:29
  • The output file is big csv file and the actual date had the same format but a string data type. And the first code was just a sample code. I have given the actual in the post now. – jithinvr Sep 26 '14 at 17:43
  • Thanks EdChum your first comment worked. Thank you. – jithinvr Sep 26 '14 at 18:01

1 Answers1

0

import datetime a=datetime.datetime.now().date()

where s will print the date only

import datetime e=datetime.datetime.now().time()

where s will print the time only