jdunck / google-visualization-python

Automatically exported from code.google.com/p/google-visualization-python
Apache License 2.0
0 stars 1 forks source link

ToCsv() generates JavaScript dates instead of spreadsheet-readable dates #15

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

Let's say you want to build a data source showing your ice cream consumption 
per day. Users should be able to download the data in CSV format, so they can 
import it into their own spreadsheets. Let's say you ate one ice cream on Jan 1 
and two ice creams on Jan 2. Start the Python interpreter and run the following 
code:
------------------------------
import datetime
import gviz_api
data = []
row = {}
row['date'] = datetime.date(2011,1,1)
row['icecreams'] = 1
data.append(row)
row = {}
row['date'] = datetime.date(2011,1,2)
row['icecreams'] = 2
data.append(row)
description = {}
description["date"] = ("date", "Date")
description["icecreams"] = ("number", "Ice creams eaten")
data_table = gviz_api.DataTable(description)
data_table.LoadData(data)
print data_table.ToCsv()
------------------------------

What is the expected output? What do you see instead?

I expected this output:
"Date", "Ice creams eaten"
"2011-01-01", 1
"2011-01-02", 2

Instead I got this output:
"Date", "Ice creams eaten"
"new Date(2011,0,1)", 1
"new Date(2011,0,2)", 2

JavaScript formatting of dates makes sense in JSON output. In CSV output it 
doesn't. ISO-formatting, YYYY-MM-DD, would make more sense as it can be read by 
spreadsheet software.

What version of the product are you using? On what operating system?

gviz_api_py-1.7.0.tar.gz
Python 2.6.5
MacOS 10.5

Original issue reported on code.google.com by momander@google.com on 30 Aug 2011 at 8:19

GoogleCodeExporter commented 9 years ago
After playing some more with the code, I found out how to export 
spreadsheet-friendly dates. You need to supply formatted values, besides the 
raw values:

import datetime
import gviz_api
data = []
row = {}
row['date'] = (datetime.date(2011,1,1), '2011-01-01')
row['icecreams'] = 1
data.append(row)
row = {}
row['date'] = (datetime.date(2011,1,2), '2011-01-02')
row['icecreams'] = 2
data.append(row)
description = {}
description["date"] = ("date", "Date")
description["icecreams"] = ("number", "Ice creams eaten")
data_table = gviz_api.DataTable(description)
data_table.LoadData(data)
print data_table.ToCsv()

Result:
"Date", "Ice creams eaten"
"2011-01-01", 1
"2011-01-02", 2

It's still a little odd that the ToCsv() method by default returns a hybrid 
Javascript/spreadsheet format that can be used neither by Javascript nor 
spreadsheets. But there is a way around it.

Given the workaround, I will let the project maintainers decide if this bug 
should be discarded or assigned a low priority.

Cheers,

/Martin

Original comment by momander@google.com on 30 Aug 2011 at 8:38

GoogleCodeExporter commented 9 years ago
Fixed in version 1.8.0

Original comment by jbas...@google.com on 27 Jan 2012 at 8:50

GoogleCodeExporter commented 9 years ago
Thanks for fixing it! The fix will make life easier for the developers who are 
just getting started with the library.

Original comment by momander@google.com on 30 Jan 2012 at 3:58