0

I am using a function to bring my date consistent in a column, it goes like this

from dateutil.parser import parse
def dateconv1(x):
   c = parse(x)
   return c

so if i will use it as, it works fine

In[15]:dateconv1("1 / 22 / 2016 15:03")
Out[15]:datetime.datetime(2016, 1, 22, 15, 3)

But when I pass it in variable like this

a= 1 / 22 / 2016 15:03
In[16]:dateconv1(str(a))

It doesn't work, so how to bring it in quotes or as a string, every help will be appreciating

4
  • 6
    You're inputing the date as an expression with division rather than as a string Commented Aug 8, 2016 at 9:27
  • That's my question- how to wrap it in like this wtih some python formaule or function, because in my column it is like 1 / 22 / 2016 15:03, and str function seems not working Commented Aug 8, 2016 at 9:32
  • Why not use parse() the way it is instead of writing a function that calls the function only? Commented Aug 8, 2016 at 9:45
  • @evans_murithi- bec Just parse will give me datetime.datetime(2016, 1, 22, 15, 3) something like this, but returning a variable at an end give me an exact time Commented Aug 8, 2016 at 9:48

2 Answers 2

1

assuming that you are talking about pandas dataset, you can use pandas to_datetime() method:

In [66]: dates = ['1 / 22 / 2016 15:03', 'Jul 22 2016 12:32PM', 'Jul 22 2016 5:40PM',
   ....:          'Jul 22 2016 8:31PM', 'Jul 23 2016 2:01PM', 'Jul 23 2016 7:24PM',
   ....:          'Jul 24 2016 4:30PM', 'Aug 1 2016 4:00PM', 'Aug 1 2016 7:49PM']

In [67]: df = pd.DataFrame({'d':dates})

In [68]: df.dtypes
Out[68]:
d    object
dtype: object

d object - means that the d column is of a string (object) dtype

In [69]: df
Out[69]:
                     d
0  1 / 22 / 2016 15:03
1  Jul 22 2016 12:32PM
2   Jul 22 2016 5:40PM
3   Jul 22 2016 8:31PM
4   Jul 23 2016 2:01PM
5   Jul 23 2016 7:24PM
6   Jul 24 2016 4:30PM
7    Aug 1 2016 4:00PM
8    Aug 1 2016 7:49PM

let's convert it to datetime dtype:

In [70]: df.d = pd.to_datetime(df.d)

In [71]: df
Out[71]:
                    d
0 2016-01-22 15:03:00
1 2016-07-22 12:32:00
2 2016-07-22 17:40:00
3 2016-07-22 20:31:00
4 2016-07-23 14:01:00
5 2016-07-23 19:24:00
6 2016-07-24 16:30:00
7 2016-08-01 16:00:00
8 2016-08-01 19:49:00

check dtype again:

In [72]: df.dtypes
Out[72]:
d    datetime64[ns]
dtype: object
Sign up to request clarification or add additional context in comments.

2 Comments

You dont even realize, You saved my lot of time from Lamdas, try except, loops, parse, datetime and so many, Thanks
@manusharma, you are welcome. :) Please tag your question with a pandas tag next time when you'll ask pandas-related questions - this way you'll attract pandas specialists and will have your questions answered much faster
0

What you have is not a valid syntax for defining a Python variable. You should wrap with '' or "" to make it a string:

a = "1 / 22 / 2016 15:03" 
dateconv1(a)

7 Comments

Thats the question- How to wrap it in ' ' or " ", when str function is not working
@manusharma If you're reading from a file, that should already come in as a string. You can't cast expressions like so with str.
It is not coming as a string, thats the problem, so how to cast or wrap it under ' ' or " " is the question
Its a dateformat variable in my dataset, now its in two different formats 2016, 1, 22, 15, 3 & Jul 22 2016 12:32PM, so with the help of parsing, I am trying to bring them in same format
Please add the dateformat variable as it is in your dataset to your question
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.