I have a key/value paired dict of (datetime/object) variables and am having trouble appending a header and dtypes to the data types.
I can make a dataframe with no column headers and dtype of object which is what I don't want. I'm trying to use the dtype and column parameters but am only being met with errors.
My code to create my dict:
for files_local in glob.glob(share_dr + '/**/*.csv', recursive=True):
match = re.search(get_matches_regex, files_local)
if match and match.group(0):
d = datetime.datetime.strptime # short form
dict_of_files_local[d(match.group('fileDate'), '%Y%m%d_%H%M%S')] = files_local
My dict when looping through:
2019-02-07 09:11:39 C:\csv\myfile_20190207_091139_092739.csv
2019-02-08 03:08:11 C:\csv\myfile_20190208_030811_031734.csv
This all works, great, but when I try to add it to a dataframe using:
df = pd.DataFrame.from_dict(dict_of_files_local, orient='index', dtype=['datetime', 'object'], columns=['Timestamp', 'Filename'])
I'm getting the error:
TypeError: data type not understood
Why is this? I thought pandas was had great datetime parsing availability?
How can I solve this problem? Am still rather new to python/pandas BTW.
Thanks a lot !
I have managed to find a workaround, which is just by passing the dict.items() to the pd.DataFrame parameter.
My code below:
df = pd.DataFrame(dict_of_files_local.items(), columns=['Timestamp', 'Filename'])
Now outputs the following:
Timestamp Filename
0 2019-02-07 09:11:39 C:\csv\myfile_20190207_091139_...
1 2019-02-08 03:08:11 C:\csv\myfile_20190208_030811_...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.