-1

I have 2 pandas df. One with the numbers and the second with the date. How can I unite them in one database so that python understand the column "date" as a date for the time series analysis?

a = [2,3,4,5,6,7]
date = [1999, 2000, 2001, 2002, 2003, 2004]
Alberto Alvarez
  • 805
  • 3
  • 11
  • 20
  • Do you have two data frames or two Series? In your example you show series, in which case you probably want to just stick them together: https://stackoverflow.com/questions/33088010/pandas-column-bind-cbind-two-data-frames. If they're both data frames, you probably want a JOIN: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.join.html – divibisan Apr 10 '18 at 21:02
  • Thank you for the reply. I have both, in the example above a present a series. I know how to merge them, but how do i define a date column so that python understands that this is a column specifying a date? I need this to perform the analysis on the time series – Alberto Alvarez Apr 10 '18 at 21:09
  • Are you asking a database question? Your question is not tagged appropriately then. If you are, the date type varies based on which database you are using. – user3483203 Apr 10 '18 at 21:14
  • Aside: "A bug in the code?" is not a particularly useful summary of your question. – DSM Apr 10 '18 at 21:14
  • So, then, are you asking how to **format** a variable as a date? – divibisan Apr 10 '18 at 21:19
  • yes. Can you help me with this, please? – Alberto Alvarez Apr 10 '18 at 21:20

1 Answers1

0

If I understand right, you are looking to create a time series in pandas with year (date having only years in your date list) and with values in the list 'a'.

Please look at the below sample and let me know if it serves the purpose.

import pandas as pd

a = [2,3,4,5,6,7]
date = [1999, 2000, 2001, 2002, 2003, 2004]

# The date list has only integers, convert these int to str, so that the to_datetime function parses it well.
date_string = map(str, date)
index = pd.to_datetime(date_string)
# only interested in the year portion of the datetime. If not, you can skip the next step and use 'index=index' directly
index_year = index.year
series = pd.Series(a,index=index_year, name='a')
print(series)
prabhakar
  • 472
  • 1
  • 4
  • 11