本文整理汇总了Python中pandas.DataFrame.from_records方法的典型用法代码示例。如果您正苦于以下问题:Python DataFrame.from_records方法的具体用法?Python DataFrame.from_records怎么用?Python DataFrame.from_records使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.DataFrame
的用法示例。
在下文中一共展示了DataFrame.from_records方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_stata_writer_pandas
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_records [as 别名]
def test_stata_writer_pandas():
buf = BytesIO()
dta = macrodata.load().data
dtype = dta.dtype
#as of 0.9.0 pandas only supports i8 and f8
dta = dta.astype(np.dtype([('year', 'i8'),
('quarter', 'i8')] + dtype.descr[2:]))
dta4 = dta.astype(np.dtype([('year', 'i4'),
('quarter', 'i4')] + dtype.descr[2:]))
dta = DataFrame.from_records(dta)
dta4 = DataFrame.from_records(dta4)
# dta is int64 'i8' given to Stata writer
writer = StataWriter(buf, dta)
writer.write_file()
buf.seek(0)
dta2 = genfromdta(buf)
dta5 = DataFrame.from_records(dta2)
# dta2 is int32 'i4' returned from Stata reader
if dta5.dtypes[1] is np.dtype('int64'):
ptesting.assert_frame_equal(dta.reset_index(), dta5)
else:
# don't check index because it has different size, int32 versus int64
ptesting.assert_frame_equal(dta4, dta5[dta5.columns[1:]])
示例2: test_datetime_roundtrip
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_records [as 别名]
def test_datetime_roundtrip():
dta = np.array([(1, datetime(2010, 1, 1), 2),
(2, datetime(2010, 2, 1), 3),
(4, datetime(2010, 3, 1), 5)],
dtype=[('var1', float), ('var2', object), ('var3', float)])
buf = BytesIO()
writer = StataWriter(buf, dta, {"var2" : "tm"})
writer.write_file()
buf.seek(0)
dta2 = genfromdta(buf)
assert_equal(dta, dta2)
dta = DataFrame.from_records(dta)
buf = BytesIO()
writer = StataWriter(buf, dta, {"var2" : "tm"})
writer.write_file()
buf.seek(0)
dta2 = genfromdta(buf, pandas=True)
ptesting.assert_frame_equal(dta, dta2.drop('index', axis=1))
示例3: fetch_DataFrame
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_records [as 别名]
def fetch_DataFrame(query,
dbhandle=None,
attach=False):
'''Fetch query results and returns them as a pandas dataframe'''
dbhandle = connect(dbhandle, attach=attach)
cc = dbhandle.cursor()
sqlresult = cc.execute(query).fetchall()
cc.close()
# see http://pandas.pydata.org/pandas-docs/dev/generated/
# pandas.DataFrame.from_records.html#pandas.DataFrame.from_records
# this method is design to handle sql_records with proper type
# conversion
field_names = [d[0] for d in cc.description]
pandas_DataFrame = DataFrame.from_records(
sqlresult,
columns=field_names)
return pandas_DataFrame
示例4: getCryptoHistoricalData
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_records [as 别名]
def getCryptoHistoricalData(self, symbol, endTime, histPeriod, vwap=False):
endTimeUNIX = dateToUNIX(endTime)
startDate = getCurrentDateStr()
priorDate = datetimeDiff(startDate, histPeriod)
gdaxTicker = GDAX_TO_POLONIEX[symbol]
stDateUNIX = dateToUNIX(priorDate)
eDateUNIX = dateToUNIX(startDate)
poloniexJsonURL = self.POLO_HIST_DATA.format(gdaxTicker, stDateUNIX, eDateUNIX, self.interval)
import json
import requests
poloniexJson = requests.get(poloniexJsonURL).json()
from pandas import DataFrame
histDataframe = DataFrame.from_records(poloniexJson)
histDataframe.drop('quoteVolume', axis=1, inplace=True)
histDataframe.drop('weightedAverage', axis=1, inplace=True)
histDataframe['date'] = histDataframe['date'].astype(float)
return histDataframe[["date", "open", "high", "low", "close", "volume"]]
示例5: get_stocks
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_records [as 别名]
def get_stocks(
self,
stockTypeList=['sha', 'shb', 'sza', 'szb'],
columns=CONST_XUEQIU_QUOTE_ORDER_COLUMN
):
for stockType in stockTypeList:
print("正在从雪球获取:{}".format(EX_NAME[stockType]))
page = 1
while True:
response = self.session.get(
URL_XUEQIU_QUOTE_ORDER(page, columns, stockType),
headers=HEADERS_XUEQIU
).json()
df = DataFrame.from_records(
response["data"], columns=response["column"])
if 'stocks' not in locals().keys():
stocks = df
else:
stocks = stocks.append(df)
if df.size == 0:
break
page += 1
return stocks
示例6: get_quotation
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_records [as 别名]
def get_quotation(self, symbol=None, symbolSet=None, dataframe=True, threadNum=3):
if 'quotation' in self.__dict__.keys():
del(self.quotation)
# Cut symbolList
symbolList = list(symbolSet)
threads = []
symbolListSlice = util.slice_list(num=threadNum, data_list=symbolList)
for symbolList in symbolListSlice:
loop = asyncio.new_event_loop()
symbolsList = util.slice_list(step=50, data_list=symbolList)
tasks = [self.get_quotation_task(
symbols=symbols) for symbols in symbolsList]
t = threading.Thread(target=util.thread_loop, args=(loop, tasks))
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
if dataframe:
self.quotation = DataFrame.from_records(self.quotation).T
return(self.quotation)
示例7: test_read_table_buglet_4x_multiindex
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_records [as 别名]
def test_read_table_buglet_4x_multiindex(self):
# see gh-6607
text = """ A B C D E
one two three four
a b 10.0032 5 -0.5109 -2.3358 -0.4645 0.05076 0.3640
a q 20 4 0.4473 1.4152 0.2834 1.00661 0.1744
x q 30 3 -0.6662 -0.5243 -0.3580 0.89145 2.5838"""
df = self.read_table(StringIO(text), sep=r'\s+')
assert df.index.names == ('one', 'two', 'three', 'four')
# see gh-6893
data = ' A B C\na b c\n1 3 7 0 3 6\n3 1 4 1 5 9'
expected = DataFrame.from_records(
[(1, 3, 7, 0, 3, 6), (3, 1, 4, 1, 5, 9)],
columns=list('abcABC'), index=list('abc'))
actual = self.read_table(StringIO(data), sep=r'\s+')
tm.assert_frame_equal(actual, expected)
示例8: test_frame_from_records_utc
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_records [as 别名]
def test_frame_from_records_utc(self):
rec = {'datum': 1.5,
'begin_time': datetime(2006, 4, 27, tzinfo=pytz.utc)}
# it works
DataFrame.from_records([rec], index='begin_time')
示例9: test_concat_datetime_datetime64_frame
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_records [as 别名]
def test_concat_datetime_datetime64_frame(self):
# #2624
rows = []
rows.append([datetime(2010, 1, 1), 1])
rows.append([datetime(2010, 1, 2), 'hi'])
df2_obj = DataFrame.from_records(rows, columns=['date', 'test'])
ind = date_range(start="2000/1/1", freq="D", periods=10)
df1 = DataFrame({'date': ind, 'test': lrange(10)})
# it works!
pd.concat([df1, df2_obj])
示例10: test_to_records_with_Mapping_type
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_records [as 别名]
def test_to_records_with_Mapping_type(self):
import email
from email.parser import Parser
compat.Mapping.register(email.message.Message)
headers = Parser().parsestr('From: <user@example.com>\n'
'To: <someone_else@example.com>\n'
'Subject: Test message\n'
'\n'
'Body would go here\n')
frame = DataFrame.from_records([headers])
all(x in frame for x in ['Type', 'Subject', 'From'])
示例11: test_resample_segfault
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_records [as 别名]
def test_resample_segfault():
# GH 8573
# segfaulting in older versions
all_wins_and_wagers = [
(1, datetime(2013, 10, 1, 16, 20), 1, 0),
(2, datetime(2013, 10, 1, 16, 10), 1, 0),
(2, datetime(2013, 10, 1, 18, 15), 1, 0),
(2, datetime(2013, 10, 1, 16, 10, 31), 1, 0)]
df = DataFrame.from_records(all_wins_and_wagers,
columns=("ID", "timestamp", "A", "B")
).set_index("timestamp")
result = df.groupby("ID").resample("5min").sum()
expected = df.groupby("ID").apply(lambda x: x.resample("5min").sum())
assert_frame_equal(result, expected)
示例12: test_read_csv_buglet_4x_multi_index2
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_records [as 别名]
def test_read_csv_buglet_4x_multi_index2(python_parser_only):
# see gh-6893
data = " A B C\na b c\n1 3 7 0 3 6\n3 1 4 1 5 9"
parser = python_parser_only
expected = DataFrame.from_records(
[(1, 3, 7, 0, 3, 6), (3, 1, 4, 1, 5, 9)],
columns=list("abcABC"), index=list("abc"))
result = parser.read_csv(StringIO(data), sep=r"\s+")
tm.assert_frame_equal(result, expected)
示例13: recarray_select
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_records [as 别名]
def recarray_select(recarray, fields):
""""
Work-around for changes in NumPy 1.13 that return views for recarray
multiple column selection
"""
from pandas import DataFrame
fields = [fields] if not isinstance(fields, (tuple, list)) else fields
if len(fields) == len(recarray.dtype):
selection = recarray
else:
recarray = DataFrame.from_records(recarray)
selection = recarray[fields].to_records(index=False)
_bytelike_dtype_names(selection)
return selection
示例14: webuse
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_records [as 别名]
def webuse(data, baseurl='http://www.stata-press.com/data/r11/', as_df=True):
"""
Download and return an example dataset from Stata.
Parameters
----------
data : str
Name of dataset to fetch.
baseurl : str
The base URL to the stata datasets.
as_df : bool
If True, returns a `pandas.DataFrame`
Returns
-------
dta : Record Array
A record array containing the Stata dataset.
Examples
--------
>>> dta = webuse('auto')
Notes
-----
Make sure baseurl has trailing forward slash. Doesn't do any
error checking in response URLs.
"""
# lazy imports
from statsmodels.iolib import genfromdta
url = urljoin(baseurl, data+'.dta')
dta = urlopen(url)
dta = BytesIO(dta.read()) # make it truly file-like
if as_df: # could make this faster if we don't process dta twice?
return DataFrame.from_records(genfromdta(dta))
else:
return genfromdta(dta)
示例15: test_stata_writer_array
# 需要导入模块: from pandas import DataFrame [as 别名]
# 或者: from pandas.DataFrame import from_records [as 别名]
def test_stata_writer_array():
buf = BytesIO()
dta = macrodata.load().data
dta = DataFrame.from_records(dta)
dta.columns = ["v%d" % i for i in range(1,15)]
writer = StataWriter(buf, dta.values)
writer.write_file()
buf.seek(0)
dta2 = genfromdta(buf)
dta = dta.to_records(index=False)
assert_array_equal(dta, dta2)