Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to remove the text Timestamp when saving DataFrame as Dictionary? #4

Closed
mrbiggleswirth opened this issue Sep 11, 2019 · 1 comment

Comments

@mrbiggleswirth
Copy link

Hi Sensei Markham, @justmarkham

I need to save my DataFrame as Dictionary in order to push my data into my database using Flask SQLAlchemy. But even when I change the DataType for column Time from Datetime to Object. My dictionary variable still contains the text Timestamp, how can I remove this text and parenthesis characters from my dictionary?

ufo_dict_2

{0: {'City': 'Ithaca', 'Colors Reported': nan, 'Shape Reported': 'TRIANGLE', 'State': 'NY', 'Time': Timestamp('1930-06-01 22:00:00')}}

.

x13_change_DataType_for_Series.py

import pandas as pd

#_______________________________________________________________________________

ufo = pd.read_csv('http://bit.ly/uforeports')

print(ufo.head())
print("")
print(type(ufo))
print("")
print("ufo.dtypes")
print(ufo.dtypes)

print("")
print("_______________________________________________________________________")
print("")

#_______________________________________________________________________________

# Convert Time datatype from Object to Datetime.

ufo['Time'] = pd.to_datetime(ufo.Time)

print(ufo.head())
print("")
print(type(ufo))
print("")
print("ufo.dtypes")
print(ufo.dtypes)

print("")
print("_______________________________________________________________________")
print("")

#_______________________________________________________________________________

# Convert DataFrame to Dictionary.

ufo_dict_1 = ufo.to_dict('index')

print("ufo_dict_1")
print("")
print(dict(list(ufo_dict_1.items())[0:3]))
print("")
print(type(ufo_dict_1))

print("")
print("_______________________________________________________________________")
print("")

#_______________________________________________________________________________

# Convert Time datatype from Datetime to Object.

ufo['Time'] = ufo.Time.astype(object)

print(ufo.head())
print("")
print(type(ufo))
print("")
print("ufo.dtypes")
print(ufo.dtypes)

print("")
print("_______________________________________________________________________")
print("")

#_______________________________________________________________________________

# Convert DataFrame to Dictionary.

ufo_dict_2 = ufo.to_dict('index')

print("ufo_dict_2")
print("")
print(dict(list(ufo_dict_2.items())[0:3]))
print("")
print(type(ufo_dict_2))

print("")
print("_______________________________________________________________________")
print("")

@mrbiggleswirth
Copy link
Author

I found the solution! : D

# http://strftime.org/

ufo['Time'] = ufo.Time.apply(lambda x: x.strftime('%Y-%m-%d %H:%M'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant