i wanted to truncate the time from this 03/01/2018 00:00 to 2018-01-03 and i tried list comprehension but working.
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
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
(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.
>>> 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
(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. :)