Python Forum
date format without time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
date format without time
#1
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
Reply
#2
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
Reply
#3
(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.
Reply
#4
ip_table['Date'] must be of type Series and not a str
https://docs.python.org/3/library/dateti...e.strptime
Reply
#5
(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/dateti...e.strptime
means i need to change the input data type before change format?
Reply
#6
>>> 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
Reply
#7
(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. :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  replace nan values by mean group by date.year, date.month wissam1974 5 2,722 Feb-19-2020, 06:25 PM
Last Post: AnkitGupta
  Time Data does not match format AshBax 2 18,256 Nov-13-2018, 12:19 PM
Last Post: AshBax
  Finding date count from a list of date range in pandas trillerducas72 0 1,112 May-24-2018, 02:30 AM
Last Post: trillerducas72
  pandas convert date/time to week okl 3 3,740 Mar-03-2018, 10:15 PM
Last Post: marsokod
  is a pandas dataframe timeseries time index in a specified range (but ignoring date)? m_lotinga 4 15,060 Dec-12-2016, 10:51 PM
Last Post: m_lotinga

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020