1

pandas.DataFrame.from_csv(filename) seems to be converting my integer index into a date.

This is undesirable. How do I prevent this?

The code shown here is a toy version of a larger problem. In the larger problem, I am estimating and writing the parameters of statistical models for each zone for later use. I thought by using a pandas dataframe indexed by zone, I could easily read back the parameters. While pickle or some other format like json might solve this problem I'd like to see a pandas solution....except pandas is converting the zone number to a date.

#!/usr/bin/python

cache_file="./mydata.csv"

import numpy as np
import pandas as pd
zones = [1,2,3,8,9,10]

def create():
    data = []
    for z in zones:
        info = {'m': int(10*np.random.rand()), 'n': int(10*np.random.rand())}
        info.update({'zone':z})
        data.append(info)
    df = pd.DataFrame(data,index=zones)
    print "about to write this data:"
    print df
    df.to_csv(cache_file)

def read():
    df = pd.DataFrame.from_csv(cache_file)
    print "read this data:"
    print df

create()
read()

Sample output:

about to write this data:
    m  n  zone
1   0  3     1
2   5  8     2
3   6  4     3
8   1  8     8
9   6  2     9
10  7  2    10

read this data:
            m  n  zone
2013-12-01  0  3     1
2013-12-02  5  8     2
2013-12-03  6  4     3
2013-12-08  1  8     8
2013-12-09  6  2     9
2013-12-10  7  2    10

The CSV file looks OK, so the problem seems to be in reading not creating.

mydata.csv

,m,n,zone
1,0,3,1
2,5,8,2
3,6,4,3
8,1,8,8
9,6,2,9
10,7,2,10

I suppose this might be useful:

pd.__version__
0.12.0

Python version is python 2.7.5+

I want to record the zone as an index so I can easily pull out the corresponding parameters later. How do I keep pandas.DataFrame.from_csv() from turning it into a date?

2
  • 2
    in pandas.DataFrame.from_csv? the parse_dates argument defaults to True. Set it to False... Commented Dec 10, 2013 at 21:39
  • Do you want to make that your answer? Commented Dec 10, 2013 at 21:43

1 Answer 1

2

Reading pandas.DataFrame.from_csv? the parse_dates argument defaults to True. Set it to False.

Sign up to request clarification or add additional context in comments.

7 Comments

Just for curiosity, what if I had some other columns that were actually dates?
set parse_dates=[<name or location of date columns>]
@TomAugspurger has it right. I like to use pd.read_csv largely due to the more extensive documentation!
Is that pd.read_csv() or is it pd.io.parsers.read_csv() ? I thought the latter was just a low level method and I should be using the DataFrame's method from_csv(). I note that there is a ton of documentation on the pd.io.parsers.read_csv()
take a look at pd.read_csv?. There is lots of info there. My understanding is similar that pd.io.parsers.read_csv is lower level.
|

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.