<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Load data, type cast, and write to a CSV file
Step1:  now run some operations on the type casted data
Step2:  Reading data
meza can read both filepaths and file-like objects. Additionally, all readers return equivalent records iterators, i.e., a generator of dictionaries with keys corresponding to the column names. Please see Readers for a complete list of available readers and recognized file types.
Step3:  Processing data
Numerical analysis (à la pandas)
In the following example, pandas equivalent methods are preceded by --&gt;.
Step4:  Note
Step5:  Text processing (à la csvkit)
In the following example, csvkit equivalent commands are preceded by --&gt;.
Step6:  Now we can read the files, manipulate the data, convert the manipulated data to json, and write the json back to a new file. Also, note that since all readers return equivalent records iterators, you can use them interchangeably (in place of read_csv) to open any supported file. E.g., read_xls, read_sqlite, etc.
Step7:  Geo processing (à la mapbox)
In the following example, mapbox equivalent commands are preceded by --&gt;.
Step8:  Now we can open the file, split the data by id, and finally convert the split data to a new geojson file-like object.
Step9:  Note
Step10:  Now we're ready to write the records data to file
Step11:  Interoperability
meza plays nicely with native and NumPy arrays out of the box
<ASSISTANT_TASK:>
Python Code:
# Create a csv file
text = (
    'col1,col2,col3\n'
    'hello,5/4/82,1\n'
    'one,1/1/15,2\n'
    'happy,7/4/92,3\n')
f = StringIO(text)
f.seek(0)
# Load the file
records = io.read_csv(f)
# Records are an iterator over the rows
row = next(records)
row
# Replace first row so as not to loose any data
records = pr.prepend(records, row)
# Guess column types. Note: `detect_types` returns a new `records`
# generator since it consumes rows during type detection
records, result = pr.detect_types(records)
result['types']
# Now type cast the records. Note: most `meza.process` functions return
# generators, so lets wrap the result in a list to view the data
casted = list(pr.type_cast(records, **result))
casted[0]
Explanation: Load data, type cast, and write to a CSV file
End of explanation
# Cut out the first column of data
cut_recs = pr.cut(casted, ['col1'], exclude=True)
cut_recs
# merge the cut rows to and get the max value of the remaining columns. 
# Note: since `merge` (by definition) will always contain just one row,
# it is returned as is (not wrapped in a generator)
merged = pr.merge(cut_recs, pred=bool, op=max)
merged
# Now write merged data back to a new csv file.
f = StringIO()
io.write(f, cv.records2csv([merged]))
f.getvalue()
Explanation: now run some operations on the type casted data
End of explanation
# Read a file like object and de-duplicate the header
f = StringIO('col,col\nhello,world\n')
records = io.read_csv(f, dedupe=True)
# View the first row
next(records) 
Explanation: Reading data
meza can read both filepaths and file-like objects. Additionally, all readers return equivalent records iterators, i.e., a generator of dictionaries with keys corresponding to the column names. Please see Readers for a complete list of available readers and recognized file types.
End of explanation
# Create some data in the same structure as what the various `read...`
# functions output
header = ['A', 'B', 'C', 'D']
data = [
    [0.5607, 0.9338, 0.4769, 0.7804],
    [0.8227, 0.2844, 0.8166, 0.7323],
    [0.4627, 0.8633, 0.3283, 0.1909],
    [0.3932, 0.5454, 0.9604, 0.6376],
    [0.3685, 0.9166, 0.9457, 0.8066],
    [0.7584, 0.6981, 0.5625, 0.3578],
    [0.8959, 0.6932, 0.2565, 0.3378]]
df = [dict(zip(header, d)) for d in data]
df[0]
# Sort records by the value of column `B` --> df.sort_values(by='B')
next(pr.sort(df, 'B')) 
# Select column `A` --> df['A']
next(pr.cut(df, ['A'])) 
# Select the first 3 rows of data --> df[0:3]
list(it.islice(df, 3))
# Select all data whose value for column `A` is less than 0.5 --> df[df.A < 0.5]
next(pr.tfilter(df, 'A', lambda x: x < 0.5)) 
Explanation: Processing data
Numerical analysis (à la pandas)
In the following example, pandas equivalent methods are preceded by --&gt;.
End of explanation
# Calculate the mean of column `A` across all data --> df.mean()['A']
pr.aggregate(df, 'A', stats.mean)['A']
# Calculate the sum of each column across all data --> df.sum()
pr.merge(df, pred=bool, op=sum) 
Explanation: Note: since aggregate and merge (by definition) return just one row,
they return them as is (not wrapped in a generator).
End of explanation
# First create a few simple csv files
f1 = StringIO('col_1,col_2,col_3\n1,dill,male\n2,bob,male\n3,jane,female\n')
f2 = StringIO('col_1,col_2,col_3\n4,tom,male\n5,dick,male\n6,jill,female\n')
f1.seek(0)
f2.seek(0) 
Explanation: Text processing (à la csvkit)
In the following example, csvkit equivalent commands are preceded by --&gt;.
End of explanation
# First, let's combine the files into one iterator --> csvstack file1.csv file2.csv
records = io.join(f1, f2, ext='csv')
next(records)
next(it.islice(records, 4, None)) 
# Now let's create a persistant records list so we can do some simple manipulations
f1.seek(0)
records = list(io.read_csv(f1))
# Sort records by the value of column `col_2` --> csvsort -c col_2 file1.csv
next(pr.sort(records, 'col_2'))
# Select column `col_2` --> csvcut -c col_2 file1.csv
next(pr.cut(records, ['col_2'])) 
# Select all data whose value for column `col_2` contains `jan`
# --> csvgrep -c col_2 -m jan file1.csv
next(pr.grep(records, [{'pattern': 'jan'}], ['col_2']))
# Convert a csv file to json --> csvjson -i 4 file1.csv
f_json = StringIO()
io.write(f_json, cv.records2json(records))
f_json.getvalue()
Explanation: Now we can read the files, manipulate the data, convert the manipulated data to json, and write the json back to a new file. Also, note that since all readers return equivalent records iterators, you can use them interchangeably (in place of read_csv) to open any supported file. E.g., read_xls, read_sqlite, etc.
End of explanation
# First create a geojson file
f = StringIO(
    '{"type": "FeatureCollection","features": ['
    '{"type": "Feature", "id": 11, "geometry": '
    '{"type": "Point", "coordinates": [10, 20]}},'
    '{"type": "Feature", "id": 12, "geometry": '
    '{"type": "Point", "coordinates": [5, 15]}}]}')
f.seek(0)
Explanation: Geo processing (à la mapbox)
In the following example, mapbox equivalent commands are preceded by --&gt;.
End of explanation
# Load the geojson file and peek at the results
records, peek = pr.peek(io.read_geojson(f))
peek[0] 
# Split the records by feature ``id`` and select the first feature --> geojsplit -k id file.geojson
splits = pr.split(records, 'id')
feature_records, name = next(splits)
name
# Convert the feature records into a GeoJSON file-like object
geojson = cv.records2geojson(feature_records)
geojson.readline()
Explanation: Now we can open the file, split the data by id, and finally convert the split data to a new geojson file-like object.
End of explanation
# First let's create a simple tsv file like object
f = StringIO('col1\tcol2\nhello\tworld\n')
f.seek(0)
# Next create a records list so we can reuse it
records = list(io.read_tsv(f))
records[0] 
Explanation: Note: you can also write back to a file as shown previously io.write('file.geojson', geojson).
Writing Data
meza can persist records to disk via the following functions:
meza.convert.records2csv
meza.convert.records2json
meza.convert.records2geojson
Each function returns a file-like object that you can write to disk via meza.io.write('/path/to/file', result).
End of explanation
# Create a csv file like object
f_out = cv.records2csv(records)
f_out.readline().strip()
# Create a json file like object
f_out = cv.records2json(records)
f_out.readline()
Explanation: Now we're ready to write the records data to file
End of explanation
# First create some records and types. Also, convert the records to a list
# so we can reuse them.
records = [{'a': 'one', 'b': 2}, {'a': 'five', 'b': 10, 'c': 20.1}]
records, result = pr.detect_types(records)
records, types = list(records), result['types']
types
# Convert records to a list of arrays
narray = cv.records2array(records, types, native=True)
narray
# Convert list of arrays back to records
next(cv.array2records(narray, native=True)) 
# Convert a 2-D array to records
arr = [(1, 4), (2, 5), (3, 6)]
data = [array('i', a) for a in arr]
data
next(cv.array2records(data, native=True)) 
Explanation: Interoperability
meza plays nicely with native and NumPy arrays out of the box
End of explanation
<END_TASK>