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
Recommended Tutorials:
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
  [Numpy] Load date/time from .txt to 'datetime64' type. water 4 405 Mar-01-2024, 11:16 PM
Last Post: Gribouillis
  Pandas read csv file in 'date/time' chunks MorganSamage 4 1,647 Feb-13-2023, 11:24 AM
Last Post: MorganSamage
  Does a pandas have a date without a time? AlekseyPython 6 4,875 Feb-10-2021, 09:24 AM
Last Post: Naheed
  replace nan values by mean group by date.year, date.month wissam1974 5 8,327 Feb-19-2020, 06:25 PM
Last Post: AnkitGupta
  Time Data does not match format AshBax 2 28,766 Nov-13-2018, 12:19 PM
Last Post: AshBax
  Finding date count from a list of date range in pandas trillerducas72 0 2,720 May-24-2018, 02:30 AM
Last Post: trillerducas72
  pandas convert date/time to week okl 3 6,630 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 19,080 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