Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9162031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:06:58+00:00 2026-06-17T14:06:58+00:00

I have a list of stockmarket data pulled from Yahoo in a pandas DataFrame

  • 0

I have a list of stockmarket data pulled from Yahoo in a pandas DataFrame (see format below). The date is serving as the index in the DataFrame. I want to write the data (including the index) out to a SQLite database.

             AAPL     GE
Date
2009-01-02  89.95  14.76
2009-01-05  93.75  14.38
2009-01-06  92.20  14.58
2009-01-07  90.21  13.93
2009-01-08  91.88  13.95

Based on my reading of the write_frame code for Pandas, it does not currently support writing the index. I’ve attempted to use to_records instead, but ran into the issue with Numpy 1.6.2 and datetimes. Now I’m trying to write tuples using .itertuples, but SQLite throws an error that the data type isn’t supported (see code and result below). I’m relatively new to Python, Pandas and Numpy, so it is entirely possible I’m missing something obvious. I think I’m running into a problem trying to write a datetime to SQLite, but I think I might be overcomplicating this.

I think I may be able to fix the issue by upgrading to Numpy 1.7 or the development version of Pandas, which has a fix posted on GitHub. I’d prefer to develop using release versions of software – I’m new to this and I don’t want stability issues confusing matters further.

Is there a way to accomplish this using Python 2.7.2, Pandas 0.10.0, and Numpy 1.6.2? Perhaps cleaning the datetimes somehow? I’m in a bit over my head, any help would be appreciated.

Code:

import numpy as np
import pandas as pd
from pandas import DataFrame, Series
import sqlite3 as db

# download data from yahoo
all_data = {}

for ticker in ['AAPL', 'GE']:
    all_data[ticker] = pd.io.data.get_data_yahoo(ticker, '1/1/2009','12/31/2012')

# create a data frame
price = DataFrame({tic: data['Adj Close'] for tic, data in all_data.iteritems()})

# get output ready for database export
output = price.itertuples()
data = tuple(output)

# connect to a test DB with one three-column table titled "Demo"
con = db.connect('c:/Python27/test.db')
wildcards = ','.join(['?'] * 3)
insert_sql = 'INSERT INTO Demo VALUES (%s)' % wildcards
con.executemany(insert_sql, data)

Result:

---------------------------------------------------------------------------
InterfaceError                            Traceback (most recent call last)
<ipython-input-15-680cc9889c56> in <module>()
----> 1 con.executemany(insert_sql, data)

InterfaceError: Error binding parameter 0 - probably unsupported type.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:07:00+00:00Added an answer on June 17, 2026 at 2:07 pm

    In recent pandas the index will be saved in the database (you used to have to reset_index first).

    Following the docs (setting a SQLite connection in memory):

    import sqlite3
    # Create your connection.
    cnx = sqlite3.connect(':memory:')
    

    Note: You can also pass a SQLAlchemy engine here (see end of answer).

    We can save price2 to cnx:

    price2.to_sql(name='price2', con=cnx)
    

    We can retrieve via read_sql:

    p2 = pd.read_sql('select * from price2', cnx)
    

    However, when stored (and retrieved) dates are unicode rather than Timestamp. To convert back to what we started with we can use pd.to_datetime:

    p2.Date = pd.to_datetime(p2.Date)
    p = p2.set_index('Date')
    

    We get back the same DataFrame as prices:

    In [11]: p2
    Out[11]: 
    <class 'pandas.core.frame.DataFrame'>
    DatetimeIndex: 1006 entries, 2009-01-02 00:00:00 to 2012-12-31 00:00:00
    Data columns:
    AAPL    1006  non-null values
    GE      1006  non-null values
    dtypes: float64(2)
    

    You can also use a SQLAlchemy engine:

    from sqlalchemy import create_engine
    e = create_engine('sqlite://')  # pass your db url
    
    price2.to_sql(name='price2', con=cnx)
    

    This allows you to use read_sql_table (which can only be used with SQLAlchemy):

    pd.read_sql_table(table_name='price2', con=e)
    #         Date   AAPL     GE
    # 0 2009-01-02  89.95  14.76
    # 1 2009-01-05  93.75  14.38
    # 2 2009-01-06  92.20  14.58
    # 3 2009-01-07  90.21  13.93
    # 4 2009-01-08  91.88  13.95
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have list of structure. I want to modify a particular data from the
i have list of items IList with data that looks list this: GenId TestMode
I have list having data like a, b, c, d, e, f, g, h,
I have list of restaurant details along with menus which i got from singleplatform.
I have list of strings var data = new List<string> {Name1, Surname1, Name2, Surname2
I have List<T> data and Predicate<T> condition. What way should I use to clear
I have list of location address. from my current location, i need to get
I have list of objects which are returned from mvc controller as Json. I'm
I have list of dict named menu_dict_list: [{'date': datetime.datetime(2011, 8, 15, 0, 0), 'Popcorn':
I have list of files. In array data provider i have fields: approved, filename,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.