While asking another question, I struggled to paste a toy DataFrame here due to the columns being pd.datetime.date and pd.datetime.time. I would like for people answering to being able to import a toy df - which is still slightly bigger than the toy of a toy df below - quickly.
I have read this and another post which explains to use df.head().to_dict()
and copy the dataframe. I did a toy example df and tried the method. This is the result of df.head().to_dict()
:
In [0]: df = pd.DataFrame({'location':['Silvretta'], 'date':[pd.to_datetime('2022-03-30 07:35:00').date()], 'time':[pd.to_datetime('2022-03-30 07:35:00').time()]})
In [1]: s = df.head().to_dict()
In [2]: print(s)
Out [2]
{'location': {0: 'Silvretta'}, 'date': {0: datetime.date(2022, 3, 30)}, 'time': {0: datetime.time(7, 35)}}
Now when I call pd.DataFrame(s)
I obtain my dataframe, but when I manually copy the string (like the people helping me would have to do) I get the following error.
In [3] pd.DataFrame(s) # works
In [4] pd.DataFrame({'location': {0: 'Silvretta'}, 'date': {0: datetime.date(2022, 3, 30)}, 'time': {0: datetime.time(7, 35)}})
Out [4]
---------------------------------------------------------------------------
[...]
NameError: name 'datetime' is not defined
Firstly, I do not get why there is a difference between the two options. Why does it make a difference?
Finally, how do I nicely format and display a DataFrame with datetimes to get help? The In [0]
is obviously oversimplified, so I would not like to 'build' my df in the next question, but just paste the final copyable version.
Thanks for the help!
I have tried to show the table by formatting a table using the StackOverflow table, but that is quite time consuming, even for a 5x6 matrix (is there a trick?) and not copyable (I think). The sharing using a dictionary does not work for me. I could post the whole way of constructing the df, but I do not want to overload the next question I am going to ask.
CodePudding user response:
If need create new DataFrame with dict use import datetime
for correct import datetimes variables:
import datetime
print (pd.DataFrame({'location': {0: 'Silvretta'},
'date': {0: datetime.date(2022, 3, 30)},
'time': {0: datetime.time(7, 35)}}))
location date time
0 Silvretta 2022-03-30 07:35:00
Explanantion:
If pass pd.DataFrame(s)
there is not parsed printed version of dict, so working well, else for parsing string representation of datetimes need import datetime
.