Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python & Pandas: Combine columns into a date

Tags:

python

pandas

In my dataframe, the time is separated in 3 columns: year, month, day, like this: enter image description here

How can I convert them into date, so I can do time series analysis?

I can do this:

df.apply(lambda x:'%s %s %s' % (x['year'],x['month'], x['day']),axis=1)

which gives:

1095       1954 1 1
1096       1954 1 2
1097       1954 1 3
1098       1954 1 4
1099       1954 1 5
1100       1954 1 6
1101       1954 1 7
1102       1954 1 8
1103       1954 1 9
1104      1954 1 10
1105      1954 1 11
1106      1954 1 12
1107      1954 1 13

But what follows?

EDIT: This is what I end up with:

from datetime import datetime
df['date']= df.apply(lambda x:datetime.strptime("{0} {1} {2}".format(x['year'],x['month'], x['day']), "%Y %m %d"),axis=1)
df.index= df['date']
like image 520
cqcn1991 Avatar asked Aug 07 '15 03:08

cqcn1991


People also ask

What is Python?

More about defining functions in Python 3 Python is a programming language that lets you work quickly and integrate systems more effectively. Learn More Whether you're new to programming or an experienced developer, it's easy to learn and use Python. Python source code and installers are available for download for all versions!

What is Python programming language?

Python is an interpreted high-level general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation. Its language constructs as well as its object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.

What is a Pythonic program?

A common neologism in the Python community is pythonic, which can have a wide range of meanings related to program style. To say that code is pythonic is to say that it uses Python idioms well, that it is natural or shows fluency in the language, that it conforms with Python's minimalist philosophy and emphasis on readability.

What is the syntax of Python?

Python has a simple syntax similar to the English language. Python has syntax that allows developers to write programs with fewer lines than some other programming languages. Python runs on an interpreter system, meaning that code can be executed as soon as it is written.


2 Answers

Here's how to convert value to time:

import datetime


df.apply(lambda x:datetime.strptime("{0} {1} {2} 00:00:00".format(x['year'],x['month'], x['day']), "%Y %m %d %H:%M:%S"),axis=1)
like image 172
FirebladeDan Avatar answered Sep 28 '22 14:09

FirebladeDan


It makes no sense to format a date to a string and immediately reparse it; use the datetime constructor instead:

df.apply(lambda x: datetime.date(x['year'], x['month'], x['day']), axis=1)
like image 44
Clément Avatar answered Sep 28 '22 14:09

Clément