import pandas as pd

fname = "../data/periods.csv"

df = pd.read_csv(fname)

df

"""
Explanation: x lines of Python
Read and write CSV files
This notebook accompanies the agilescientific.com blog post of the same name:

x lines of Python: read and write CSV

Introduction
CSV files are the de facto standard way to store data on the web. They are human-readable, easy to parse with multiple tools, and they compress easily. So you need to know how to read and write them in Python.
Nine times out of ten, the way to read and write CSV files with Python is with pandas. We'll do that first. But that's not always an option (maybe you don't want the dependency in your code), so we'll do it lots of ways:

With pandas
2 ways with the built-in package csv...
csv.reader
csv.DictReader 


With NumPy.

We'll also use pandas to read a couple of CSV files from the web (as opposed to from flat files on our computer).
Using pandas
Using pd.read_csv.
End of explanation
"""


df[df.name=="Permian"].start

"""
Explanation: We can get the start of the Permian like this:
End of explanation
"""


df.loc[df.name=='Cretaceous', 'start'] = 145.0
df.loc[df.name=='Cretaceous', 'start']

"""
Explanation: Let's fix the start of the Cretaceous:
End of explanation
"""


df.to_csv("../data/pdout.csv")

"""
Explanation: After you have changed or added to a DataFrame, pandas also makes it very easy to write a CSV file containing your data.
End of explanation
"""


import csv

with open(fname) as f:
    reader = csv.reader(f)
    data = [row for row in reader]

data

[d[2] for d in data if d[0]=="Permian"]

"""
Explanation: Using csv.reader
Docs for the ordinary reader.
End of explanation
"""


with open(fname) as f:
    reader = csv.DictReader(f)
    data = [row for row in reader]

data

[d['start'] for d in data if d['name']=="Permian"]

"""
Explanation: Note that we needed to know the positions of the items in the rows, which we could only get by inspection. We could skip that header row if we wanted to, but there's a better way: use the header as the keys in a dictionary...
Using csv.DictReader
Docs for the DictReader.
End of explanation
"""


import requests
import io

df = pd.read_csv('https://raw.githubusercontent.com/agile-geoscience/xlines/master/data/periods.csv')

df.head()

"""
Explanation: There is a corresponding DictWriter class for writing CSVs.
Note that pandas has lots of file readers, including ones for:

Excel files
JSON
SAS
Stata

It can even read the clipboard!
Bonus: reading a CSV file from the web
For example, this one is hosted by GitHub. It's publicly readable, so we don't need to authenticate.
In the X Lines of Python: Machine Learning notebook, I read the online file into a buffer, but it turns out you don't need to do this &mdash; you can just give pd.read_csv() a URL!
End of explanation
"""


import numpy as np

x = np.genfromtxt(fname, delimiter=',', skip_header=1, usecols=[2,3])
x

"""
Explanation: Bonus: using NumPy
pandas is perfect for this CSV because it's really a table, containing a mixture of data types (strings and floats).
Nonetheless, we can read it as an array... I'm not really into 'named arrays', so I'll just read the two numeric columns.
We'll use np.genfromtxt.
End of explanation
"""


np.savetxt("../data/npout.csv", x, delimiter=",", header="start,end")

"""
Explanation: We can write a CSV like so:
End of explanation
"""


key = "PUT YOUR KEY HERE"

import json

url = "https://sheets.googleapis.com/v4/spreadsheets/{id}/values/{sheet}"

meta = {"id": "1YlnEGT8uHpRllk7rjAgFFl8V6B5-kl02DBie11PjG9Q",
        "sheet": "Sheet1"
        }
url = url.format(**meta)

params = {"key": key}

r = requests.get(url, params=params)
j = json.loads(r.text)['values']
df = pd.DataFrame(j[1:], columns=j[0])
df.head()

"""
Explanation: Bonus: reading a CSV file from Google Docs
It used to be easy to anonymously read a public file directly from Google Docs, but now you need an API key. It's not too hard to set up, but you'll need to read some docs.
When you have an API key, put it here...
End of explanation
"""


