質問編集履歴

3

内容追記

2018/11/27 02:20

投稿

---stax---
---stax---

スコア148

test CHANGED
File without changes
test CHANGED
@@ -153,3 +153,93 @@
153
153
  ```
154
154
 
155
155
  excelに出力する際に時間の情報は保持させたいのですが良い方法はありますでしょうか・・・
156
+
157
+
158
+
159
+
160
+
161
+ --再追記--
162
+
163
+
164
+
165
+ display(rentals.head())の結果
166
+
167
+ ![display(rentals.head())](2da5853c66b3e21829c248dc14593eb3.png)
168
+
169
+
170
+
171
+ display(rentals.info())の結果
172
+
173
+ ![display(rentals.info())](33b3c640a85d9160e9ae05016e5361a7.png)
174
+
175
+
176
+
177
+ 試しにcsvで出力しようとto_csvで出力し、欠損値が含まれているところには文字列’None’を入れる部分も追加したいと考え以下のコードを作成しました
178
+
179
+ ```python
180
+
181
+ import psycopg2
182
+
183
+ import sys
184
+
185
+ import pandas as pd
186
+
187
+ import openpyxl
188
+
189
+
190
+
191
+ # 接続情報
192
+
193
+ connection_config = {
194
+
195
+ 'host': 'localhost',
196
+
197
+ 'port': '5432',
198
+
199
+ 'database': 'postgres',
200
+
201
+ 'user': 'postgres',
202
+
203
+ 'password': 'postgres'
204
+
205
+ }
206
+
207
+
208
+
209
+ # 接続
210
+
211
+ connection = psycopg2.connect(**connection_config)
212
+
213
+
214
+
215
+ # DataFrameでロード
216
+
217
+ data = pd.read_sql(sql="SELECT * FROM testdata.prac_data;", con=connection)
218
+
219
+
220
+
221
+ # 欠損値→Noneに置き換え
222
+
223
+ data = data.fillna('None')
224
+
225
+
226
+
227
+ # 出力
228
+
229
+ data.to_csv(r'C:\Users\Desktop\test.csv' , encoding="SHIFT-JIS")
230
+
231
+
232
+
233
+
234
+
235
+ excel_output()
236
+
237
+ ```
238
+
239
+ しかしこちらも
240
+
241
+ AttributeError: 'Timestamp' object has no attribute 'copy'
242
+
243
+ というエラーがdata = data.fillna('None')の部分で発生し行き詰ってしまいました・・・
244
+
245
+ xlsx形式、csv形式のどちらも将来使うことになるので可能であればこちらの対処法もご教授頂けたら嬉しいです。

2

内容追記

2018/11/27 02:20

投稿

---stax---
---stax---

スコア148

test CHANGED
File without changes
test CHANGED
@@ -78,6 +78,78 @@
78
78
 
79
79
 
80
80
 
81
- [参考](https://qiita.com/takahi/items/c9b7fc01cdccd4b7f661)
81
+ [参考リンク](https://qiita.com/takahi/items/c9b7fc01cdccd4b7f661)
82
82
 
83
- [公式](http://initd.org/psycopg/docs/cursor.html?highlight=copy#cursor.copy_to)
83
+ [公式サイト](http://initd.org/psycopg/docs/cursor.html?highlight=copy#cursor.copy_to)
84
+
85
+
86
+
87
+ --2018/11/27 追記--
88
+
89
+
90
+
91
+ pandasにread_sqlメソッドがあることを知りそちらを使ってみました
92
+
93
+ ```python
94
+
95
+ import psycopg2
96
+
97
+ import sys
98
+
99
+ import pandas as pd
100
+
101
+ import openpyxl
102
+
103
+
104
+
105
+ # 接続情報
106
+
107
+ connection_config = {
108
+
109
+ 'host': 'localhost ',
110
+
111
+ 'port': '5432',
112
+
113
+ 'database': 'postgres',
114
+
115
+ 'user': 'postgres',
116
+
117
+ 'password': 'postgres'
118
+
119
+ }
120
+
121
+
122
+
123
+ # 接続
124
+
125
+ connection = psycopg2.connect(**connection_config)
126
+
127
+
128
+
129
+ # DataFrameでロード
130
+
131
+ rentals = pd.read_sql(sql="SELECT * FROM testdata.prac_data;", con=connection)
132
+
133
+
134
+
135
+ # 表示
136
+
137
+ rentals
138
+
139
+
140
+
141
+ rentals.to_excel(r'C:\Users\Desktop\test.xlsx')
142
+
143
+ ```
144
+
145
+
146
+
147
+ ただ、これを実行すると以下のエラーが発生します
148
+
149
+ ```python
150
+
151
+ TypeError: Excel doesn't support timezones in datetimes. Set the tzinfo in the datetime/time object to None or use the 'remove_timezone' Workbook() option
152
+
153
+ ```
154
+
155
+ excelに出力する際に時間の情報は保持させたいのですが良い方法はありますでしょうか・・・

1

参考リンク追加

2018/11/27 00:20

投稿

---stax---
---stax---

スコア148

test CHANGED
File without changes
test CHANGED
@@ -75,3 +75,9 @@
75
75
  また、要件がxlsxファイルでの出力なのでひとまずcsv形式で出力してから変換しても良いかなと考えているのでもっとほかのやり方もあれば教えてほしいです。
76
76
 
77
77
  宜しくお願い致します。
78
+
79
+
80
+
81
+ [参考](https://qiita.com/takahi/items/c9b7fc01cdccd4b7f661)
82
+
83
+ [公式](http://initd.org/psycopg/docs/cursor.html?highlight=copy#cursor.copy_to)