Python Forum
date format without time - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: Data Science (https://python-forum.io/Forum-Data-Science)
+--- Thread: date format without time (/Thread-date-format-without-time)



date format without time - issac_n - Jan-15-2018

i wanted to truncate the time from this 03/01/2018 00:00 to 2018-01-03 and i tried list comprehension but working. Doh
any idea?
        filename = filedialog.askopenfilename( initialdir="C:/", title="Select CSV file", filetypes=(("CSV file", "*.csv"), ("all files", "*.*")))
        ip_table = pd.read_csv(filename)
        ip_table['DateB']=(datetime.strptime(ip_table['Date'], '%Y-%m-%d %H:%M').strftime('%Y-%m-%d') for ip_table['DateB'] in ip_table['Date'])
        print(ip_table)
error msg as below;
https://ibb.co/gGXsXR


RE: date format without time - buran - Jan-15-2018

it's not an error - when you use () brackets in comprehension expression (line#3) it produce a generator. use [] in order to get a list


RE: date format without time - issac_n - Jan-15-2018

(Jan-15-2018, 10:56 AM)buran Wrote: it's not an error - when you use () brackets in comprehension expression (line#3) it produce a generator. use [] in order to get a list
an error occurs >>
Error:
ip_table['DateB']=[datetime.strptime(ip_table['Date'], '%Y-%m-%d %H:%M').strftime('%Y-%m-%d') for ip_table['DateB'] in ip_table['Date']] TypeError: strptime() argument 1 must be str, not Series
i don't understand the argument error.


RE: date format without time - metulburr - Jan-15-2018

ip_table['Date'] must be of type Series and not a str
https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime


RE: date format without time - issac_n - Jan-15-2018

(Jan-15-2018, 02:19 PM)metulburr Wrote: ip_table['Date'] must be of type Series and not a str https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime
means i need to change the input data type before change format?


RE: date format without time - snippsat - Jan-15-2018

>>> from datetime import datetime
>>> d = ['11-01-2018']
>>> datetime.strptime(d, '%d-%m-%Y')
Traceback (most recent call last):
  File "<string>", line 301, in runcode
  File "<interactive input>", line 1, in <module>
TypeError: strptime() argument 1 must be str, not list

# Fix
>>> datetime.strptime(d[0], '%d-%m-%Y')
datetime.datetime(2018, 1, 11, 0, 0)
You see that this generate the same error except it's a list,
i fix it be taking the string out list using index.
For Pandas Series object there is a pandas.Series.to_string


RE: date format without time - issac_n - Jan-19-2018

(Jan-15-2018, 03:59 PM)snippsat Wrote:
 >>> from datetime import datetime >>> d = ['11-01-2018'] >>> datetime.strptime(d, '%d-%m-%Y') Traceback (most recent call last): File "<string>", line 301, in runcode File "<interactive input>", line 1, in <module> TypeError: strptime() argument 1 must be str, not list # Fix >>> datetime.strptime(d[0], '%d-%m-%Y') datetime.datetime(2018, 1, 11, 0, 0)
You see that this generate the same error except it's a list, i fix it be taking the string out list using index. For Pandas Series object there is a pandas.Series.to_string


thanks for info, i changed the input format before loading to it. :)